2012-07-04 16 views
15

¿Hay alguna forma de obtener datos de la "Notificación de GCM"? Aquí hay una parte de mi cadena json que envío con gcm: {"data":{"id":"123"}}. Necesito obtener valor de identificación en mi aplicación, pero no sé cómo ... muchas gracias.Obtener datos de la notificación de GCM

Respuesta

26

Si está utilizando la nueva biblioteca de GCM, entonces necesita crear una clase que amplíe IntentService, aquí es donde la biblioteca de GCM le notificará cuando se reciba un mensaje de GCM. Por favor, eche un vistazo a la muestra MyIntentService.java:

@Override 
public final void onHandleIntent(Intent intent) { 
    try { 
     String action = intent.getAction(); 
     if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { 
      handleRegistration(intent); 
     } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { 
      handleMessage(intent); 
     } 
    } finally { 
     synchronized(LOCK) { 
      sWakeLock.release(); 
     } 
    } 
} 

private void handleMessage(Intent intent) { 
    String id = intent.getExtra("id"); 
} 

Si no está utilizando la biblioteca de GCM, entonces la respuesta GCM está llegando a usted en un intento de su receptor, entonces se puede utilizar getExtras de intención(). getString() para recuperar el par de clave/valor de su notificación de GCM. p.ej.

// intent come in in your onReceive method of your BroadcastReceiver: 
public onReceive(Context context, Intent intent) { 
    // check to see if it is a message 
    if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { 
     String id = intent.getExtras().getString("id"); 
     String other_key = intent.getExtras().getString("other_key"); 

     // if your key/value is a JSON string, just extract it and parse it using JSONObject 
     String json_info = intent.getExtras().getString("json_info"); 
     JSONObject jsonObj = new JSONObject(json_info);   
    } 
} 
+0

Ok, pero ¿cómo puedo llegar hasta aquí (onMessage (contexto Contexto protected void, la intención) Intención) cadena de notificación? – mysho

+0

Actualicé mi respuesta, eche un vistazo. – azgolfer

+0

¿Cómo puedo obtener gcm_notification_string de Intent? –

6

La mejor manera de obtenerlo como una representación json es agregar sus datos como un objeto json.

{ 
    "registration_ids" : [ 
     "id1", 
     "id2" 
    ], 
    "data" : { 
     "my_json_object": { 
      "text" :"This is my message", 
      "title":"Some title" 
     } 
    }, 
    "collapse_key":"12345" 
} 

Luego de analizar el objeto, simplemente:

String json = getIntent().getExtras().getString("my_json_object"); 
JsonObject jObject = new JsonObject(json); 
+0

y la mejor manera que he encontrado hasta ahora para "agregar sus datos como un objeto json" es: 'String json = gson.toJson ([YOUR_OBJECT]); Message msg = new Message.Builder(). AddData ("mensaje", json) .build(); gcmresult = sender.send (msg, [SU ID DE REG CLIENTE DE GCM], 5); ' –

0
<receiver android:name=".beforelogin.GcmBroadcastReceiver" 
      android:permission="com.google.android.c2dm.permission.SEND"> 
    <intent-filter> 
     <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
     <category android:name="android.intent.category.TAB" /> 
    </intent-filter> 
</receiver> 
<service android:name=".beforelogin.GcmIntentService" /> 
<meta-data android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 
+0

Agregue su clase GCmBroadcastReceviver y clase GcmIntentService para recibir notificaciones –

Cuestiones relacionadas