Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android GCM - "Handling Received Data"

Android documentation for GCM here states

that key-pair values in the data parameter, they are available as extras in this intent, with the keys being the extra names.

private void handleMessage(Intent intent) {
    // server sent 2 key-value pairs, score and time
    String score = intent.getExtra("score");
    String time = intent.getExtra("time");
    // generates a system notification to display the score and time
}

But intent.getExtra() method does not accept an argument

public Bundle getExtras ()

Since: API Level 1
Retrieves a map of extended data from the intent.

Returns
the map of all extras previously added with putExtra(), or null if none have been added.

MyQuestion

How to retrieve a 'String' from the GCM message in onMessage() method?

P.S onMessage(Context context, Intent intent): Called when your server sends a message to GCM, and GCM delivers it to the device. If the message has a payload, its contents are available as extras in the intent.

like image 528
Gaurav Agarwal Avatar asked Dec 27 '22 20:12

Gaurav Agarwal


1 Answers

You should use:

intent.getExtras().getString("score");
intent.getExtras().getString("time");

Be careful about the type, it can be:

intent.getExtras().getInt("myvar");

Or some other types. Take a look at Bundle.

like image 130
thomasg Avatar answered Jan 09 '23 05:01

thomasg