getNotification()。getBody()失败

时间:2016-05-29 17:32:08

标签: android push-notification firebase firebase-cloud-messaging

我正在尝试从google firebase通知消息中提取消息。

这是我的代码:

remoteMessage.getFrom()

remoteMessage.getNotification().getBody()返回正确的值,.example.mturky.testfirebase W/dalvikvm: threadid=16: thread exiting with uncaught exception (group=0x4199ccf8) 05-29 18:26:09.886 15436-15583/com.example.mturky.testfirebase W/dalvikvm: threadid=16: uncaught exception occurred 05-29 18:26:09.887 15436-15583/com.example.mturky.testfirebase W/System.err: java.lang.NullPointerException 05-29 18:26:09.887 15436-15583/com.example.mturky.testfirebase W/System.err: at com.example.mturky.testfirebase.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:31) 05-29 18:26:09.888 15436-15583/com.example.mturky.testfirebase W/System.err: at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source) 05-29 18:26:09.888 15436-15583/com.example.mturky.testfirebase W/System.err: at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source) 05-29 18:26:09.888 15436-15583/com.example.mturky.testfirebase W/System.err: at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source) 05-29 18:26:09.888 15436-15583/com.example.mturky.testfirebase W/System.err: at com.google.firebase.iid.zzb$2.run(Unknown Source) 05-29 18:26:09.888 15436-15583/com.example.mturky.testfirebase W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 05-29 18:26:09.889 15436-15583/com.example.mturky.testfirebase W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 05-29 18:26:09.889 15436-15583/com.example.mturky.testfirebase W/System.err: at java.lang.Thread.run(Thread.java:841) 05-29 18:26:09.889 15436-15583/com.example.mturky.testfirebase W/dalvikvm: threadid=16: calling UncaughtExceptionHandler 05-29 18:26:09.890 15436-15583/com.example.mturky.testfirebase E/AndroidRuntime: FATAL EXCEPTION: pool-4-thread-1 Process: com.example.mturky.testfirebase, PID: 15436 java.lang.NullPointerException at com.example.mturky.testfirebase.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:31) at com.google.firebase.messaging.FirebaseMessagingService.zzo(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzn(Unknown Source) at com.google.firebase.messaging.FirebaseMessagingService.zzm(Unknown Source) at com.google.firebase.iid.zzb$2.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:841) 崩溃时显示以下日志:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_xlim(1,3) 
ax.set_ylim(1,3)
ax.plot([2.5],[2.5],marker=r'$\circlearrowleft$',ms=100)
plt.show()

任何想法如何修复它?

5 个答案:

答案 0 :(得分:8)

扩展我在帖子下发表的评论:

remoteMessage.getNotification().getBody()仅在您发送通知消息时才可用。使用firebase控制台或server-api与
payload = { "notification" : { "body" : "my body"} }

如果您要发送数据消息,例如{ "data" : { "mykey" : "myvalue"} },那么在客户端上您应该使用以下方法:remoteMessage.getData()

答案 1 :(得分:0)

答案是:(来自@DiegoGiorgini):

getBody仅在您发送消息时设置选项通知:{body:“my body”}时可用。如果要发送数据有效负载,则应使用方法getData()

答案 2 :(得分:0)

  showNotification(remoteMessage.getData().put("","message"));

如果通知到服务器端而不是控制台,这将有效。

答案 3 :(得分:0)

Your post json object should look like below

Headers:

Content-Type : application/json
Authorization: key=API KEY

JSON Object

{

    "notification": {
        "title": "Title for the message",
        "body": "message content here"
    },
    "to": "registration id"
}

and use below line to call showNotification method as below in java.

showNotification(remoteMessage.getData().put("","message"));

答案 4 :(得分:0)

 $message = array(
        'registration_ids' => $registrationIDs,
        'data' => array(
                "message" => $messageText,
                "id" => $id,
        ),
     'notification' => array(
                "body" => "body of notification",
                "title" => "title for notification",
        )
    );