【问题标题】:Get data from GCM notification从 GCM 通知中获取数据
【发布时间】:2012-07-04 08:49:51
【问题描述】:

有没有办法从“GCM 通知”中获取数据。这是我用 gcm 发送的 json 字符串的一部分:{"data":{"id":"123"}}。我需要在我的应用程序中获取 id 的值,但我不知道如何......非常感谢。

【问题讨论】:

    标签: android notifications google-cloud-messaging


    【解决方案1】:

    如果您使用的是新的 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");
    }
    

    如果您没有使用 GCM 库,那么 GCM 响应会以您的接收器中的意图发送给您,那么您可以使用意图的 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);          
       }
    }
    

    【讨论】:

    • 好的,但是我怎样才能到达这里(受保护的无效onMessage(上下文上下文,意图意图))通知字符串?
    • 更新了我的答案,看看吧。
    • 如何从 Intent 获取 gcm_notification_string?
    • @ZuzooVn,请参阅我的更新答案。我在之前的答案中犯了一个小错误。您无法从 GCM 服务器中发送的内容中获取原始字符串,因为操作系统会将其解析为键/值对并将其作为意图发送给您的接收者。但是,您可以将 JSON 字符串分配给一个键,然后在您的应用程序中对其进行解码。
    • 有没有办法将bundle解析成json?:)
    【解决方案2】:

    将其作为 json 表示的最佳方式是将数据添加为 json 对象。

    {
        "registration_ids" : [
            "id1",
            "id2"
        ],
        "data" : {
            "my_json_object": {
                "text" :"This is my message",
                "title":"Some title"
            }
        },
        "collapse_key":"12345"
    }
    

    然后只解析你的对象:

    String json = getIntent().getExtras().getString("my_json_object");
    JsonObject jObject = new JsonObject(json);
    

    【讨论】:

    • 到目前为止,我发现“将您的数据添加为 json 对象”的最佳方法是:String json = gson.toJson([YOUR_OBJECT]); Message msg = new Message.Builder().addData("message", json).build(); gcmresult = sender.send(msg, [YOUR GCM CLIENT REG ID],5);
    【解决方案3】:
    <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" />
    

    【讨论】:

    • 添加您的 GCmBroadcastReceviver 类和 GcmIntentService 类以接收通知
    猜你喜欢
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 2018-07-08
    相关资源
    最近更新 更多