Android 从 GCM 通知中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11325580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get data from GCM notification
提问by mysho
Is there a way to get data from "GCM notification". Here is a part of my json string which I send with gcm: {"data":{"id":"123"}}
. I need get value of id in my app, but I don't know how ... thanks a lot.
有没有办法从“GCM通知”中获取数据。这里是我的JSON字符串的一部分,我与GCM发送:{"data":{"id":"123"}}
。我需要在我的应用程序中获取 id 的值,但我不知道如何......非常感谢。
回答by azgolfer
If you are using the new GCM library, then you need to create a class that extends IntentService, this is where the GCM library will notify you when a GCM message is received. Please take a look at MyIntentService.java sample:
如果您正在使用新的 GCM 库,那么您需要创建一个扩展 IntentService 的类,这是 GCM 库在收到 GCM 消息时通知您的地方。请看一下 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");
}
If you are not using the GCM library, then the GCM response is coming to you in an intent in your receiver, then you can use intent's getExtras().getString() to retrieve the key/value pair from your GCM notification. e.g.
如果您没有使用 GCM 库,那么 GCM 响应会在您的接收器中以 Intent 的形式出现,然后您可以使用 Intent 的 getExtras().getString() 从 GCM 通知中检索键/值对。例如
// 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);
}
}
回答by Roberto B.
The best way to get it as a json representation is adding your data as a json object.
将其作为 json 表示的最佳方法是将您的数据添加为 json 对象。
{
"registration_ids" : [
"id1",
"id2"
],
"data" : {
"my_json_object": {
"text" :"This is my message",
"title":"Some title"
}
},
"collapse_key":"12345"
}
Then to parse your object just:
然后解析你的对象:
String json = getIntent().getExtras().getString("my_json_object");
JsonObject jObject = new JsonObject(json);
回答by Venkatesh
<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" />