获得从GCM通知数据 [英] Get data from GCM notification

查看:247
本文介绍了获得从GCM通知数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方式来获得GCM的通知的数据。下面是我送与GCM我的JSON字符串的一部分: {数据:{ID:123}} 。我需要让我的应用程序ID的价值,但我不知道怎么......非常感谢。

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库,那么你需要创建一个继承IntentService一类,这是哪里的GCM库会在一个​​GCM消息通知您被接收。请看看MyIntentService.java示例:

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:

@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");
}

如果您不使用GCM库,那么GCM响应来你意图在您的接收器,那么你可以使用意向的getExtras()的getString()从您的GCM通知检索键/值对。例如,

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.

// 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);          
   }
}

这篇关于获得从GCM通知数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆