GCM字符串而不是布尔值

时间:2013-04-16 21:44:59

标签: java android json boolean google-cloud-messaging

我收到了来自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);


    }

1 个答案:

答案 0 :(得分:14)

当邮件发送给您时,它是一个字符串,引自{{3>} 发送邮件,请求格式'数据'部分:

  

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