4

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
}

但是 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.

我的问题

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

附言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.

4

1 回答 1

10

你应该使用:

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

注意类型,它可以是:

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

或者其他一些类型。看看Bundle

于 2012-07-27T22:37:53.727 回答