2

我收到了来自 GCM 的消息,但我得到的是字符串而不是布尔值。我的 JSON 数组似乎有问题。我收到警告信息:

04-17 00:41:04.058: W/Bundle(6702): Key alarm expected Boolean but value was a java.lang.String.  The default value false was returned.
04-17 00:41:04.058: W/Bundle(6702): Attempt to cast generated internal exception:
04-17 00:41:04.058: W/Bundle(6702): java.lang.ClassCastException: java.lang.String
04-17 00:41:04.058: W/Bundle(6702):     at android.os.Bundle.getBoolean(Bundle.java:786)
04-17 00:41:04.058: W/Bundle(6702):     at android.content.Intent.getBooleanExtra(Intent.java:3282)
04-17 00:41:04.058: W/Bundle(6702):     at com.rolandas.lookup.GCMIntentService.onMessage(GCMIntentService.java:71)
04-17 00:41:04.058: W/Bundle(6702):     at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
04-17 00:41:04.058: W/Bundle(6702):     at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-17 00:41:04.058: W/Bundle(6702):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 00:41:04.058: W/Bundle(6702):     at android.os.Looper.loop(Looper.java:130)
04-17 00:41:04.058: W/Bundle(6702):     at android.os.HandlerThread.run(HandlerThread.java:60)

PHP 发送脚本。

   $devices = array();
   $data = array();
   array_push($devices, getRegId($user_id, $mysqli));
   $data = array("alarm" => true);

   SendGCM(true, $devices, $data);

JSON消息:

{"registration_ids":["APA91bFjtwqq2q5Ji88JjLjuivAzNVGxLDYXaIahCeRUOmaD6vb0T5C22uQSlygztpq_KsVGCZ-0TKQTqUsmp0PU4FXjKYfdsfsdfaxRM1gQ7oh5xHf576-JwQ9FuItsBvhQeQmiKoX3UIi0s3onBh9vO7wI3_Pvw"],"data":{"alarm":true}}

最后是我的 OnMessage 方法:

@Override
    protected void onMessage(Context context, Intent intent) {

        boolean alarm = false;

        alarm = intent.getBooleanExtra("alarm", false);


    }
4

1 回答 1

14

到您收到消息时,它是一个字符串,引用了Google 在线文档在“发送消息”下的请求格式“数据”部分:

这些值可以是任何 JSON 对象,但我们建议使用字符串,因为无论如何这些值都会在 GCM 服务器中转换为字符串。如果要包含对象或其他非字符串数据类型(如整数或布尔值),则必须自己转换为字符串。

于 2013-04-17T11:26:34.800 回答