0

我有以下代码:

@Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        Bundle extras = intent.getExtras();
        Log.i(TAG, "===> " + extras.toString());
        if (extras != null) {
            String message = (String) extras.get("mainKey");
            // notifies user
            Log.i(TAG, message);
        }
    }

我可以从服务器获取消息,但我认为extras.get("mainKey");有问题,因为它返回 null(我认为)。Logcat 显示此日志:

05-07 16:01:53.666: I/GCMIntentService(10959): Received message
05-07 16:01:53.671: I/GCMIntentService(10959): ===> Bundle[{title=Hesam has sent you a message., collapse_key=do_not_collapse, conversation_id=90, msg_id=456, user_id=22, from=1014390164590, type=message}]
05-07 16:01:53.671: V/GCMBaseIntentService(10959): Releasing wakelock
05-07 16:01:53.676: W/dalvikvm(10959): threadid=33: thread exiting with uncaught exception (group=0x40c571f8)
05-07 16:01:53.686: E/AndroidRuntime(10959): FATAL EXCEPTION: IntentService[GCMIntentService-1014390164590-1]
05-07 16:01:53.686: E/AndroidRuntime(10959): java.lang.NullPointerException: println needs a message
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.util.Log.println_native(Native Method)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.util.Log.i(Log.java:190)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at com.belldigital.utility.Log.i(Log.java:16)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at com.belldigital.fancied.GCMIntentService.onMessage(GCMIntentService.java:71)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:223)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.os.Looper.loop(Looper.java:137)
05-07 16:01:53.686: E/AndroidRuntime(10959):    at android.os.HandlerThread.run(HandlerThread.java:60)

我可以看到我从服务器发送的内容,但extras.get("mainKey");没有返回任何内容。有没有人遇到过这个问题?任何建议将不胜感激。谢谢。

4

1 回答 1

0

利用 intent.getExtras().getString("mainKey");

并确保您的使用

 $message= array("mainKey" => $message);

然后将其作为字段中的数据传递

$fields = array(
 'registration_ids' => $registation_ids,
 'data' => $message,
);

从服务器发送消息时

于 2013-05-07T10:51:58.217 回答