getNotification().getBody() 失败

getNotification().getBody() Fails

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

这是我的代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

    Log.d("FROM", "From: " + remoteMessage.getFrom() );
    sendNotification(remoteMessage.getNotification().getBody());
}

remoteMessage.getFrom() returns 正确的值,而 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.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.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)

知道如何解决吗?

答案是:(来自@DiegoGiorgini):

getBody 仅在您设置选项 notification: { body: "my body"} 时可用。如果您要发送数据负载,您应该使用方法 getData()

扩展我在问题下发表的评论:

remoteMessage.getNotification().getBody() 仅在您发送 通知消息 时可用。使用 firebase 控制台或服务器-api with
有效负载 = { "notification" : { "body" : "my body"} }

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

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

如果通知到达服务器端而不是控制台,这将起作用。

您的 post json object 应如下所示

Headers:

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

JSON Object

{

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

并使用下面的行调用 showNotification 方法,如下所示 java。

showNotification(remoteMessage.getData().put("","message"));
sendNotification(remoteMessage.getNotification().getBody()); 
  • 这是针对开发者控制台的默认推送通知。

你应该使用

sendNotification(remoteMessage.getData().get("message"));
  • 这是针对来自任何服务器的自定义推送,"message" 表示为服务器端。
 $message = array(
        'registration_ids' => $registrationIDs,
        'data' => array(
                "message" => $messageText,
                "id" => $id,
        ),
     'notification' => array(
                "body" => "body of notification",
                "title" => "title for notification",
        )
    );