Android GCM - "处理收到的数据"

Gau*_*wal 4 android google-cloud-messaging

对于GCM Android文档在这里声明

数据参数中的键对值,它们在此意图中作为附加项提供,键是额外的名称.

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
}
Run Code Online (Sandbox Code Playgroud)

但是intent.getExtra()方法不接受参数

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.
Run Code Online (Sandbox Code Playgroud)

我的问题

如何从onMessage()方法中的GCM消息中检索"字符串" ?

PS 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.

tho*_*asg 10

你应该使用:

intent.getExtras().getString("score");
intent.getExtras().getString("time");
Run Code Online (Sandbox Code Playgroud)

注意类型,它可以是:

intent.getExtras().getInt("myvar");
Run Code Online (Sandbox Code Playgroud)

或者其他一些类型.看看Bundle吧.

  • 因为getExtras()为您提供了一个Bundle. (2认同)