3

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

这是我的代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

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

崩溃时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)

知道如何解决吗?

4

6 回答 6

10

要扩展我在该问题下发布的评论:

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

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

于 2016-05-30T06:24:14.903 回答
4
sendNotification(remoteMessage.getNotification().getBody()); 
  • 这是开发者控制台默认推送通知。

你应该使用

sendNotification(remoteMessage.getData().get("message"));
  • 这是用于来自任何服务器的自定义推送,“消息”由服务器端表示。
于 2017-02-24T09:06:16.663 回答
1

您的 post json 对象应如下所示

标题:

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

JSON 对象

{

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

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

showNotification(remoteMessage.getData().put("","message"));
于 2016-10-20T13:05:46.920 回答
0

答案是:(@DiegoGiorgini):

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

于 2016-05-30T06:17:55.117 回答
0
  showNotification(remoteMessage.getData().put("","message"));

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

于 2016-09-20T09:39:08.213 回答
0
 $message = array(
        'registration_ids' => $registrationIDs,
        'data' => array(
                "message" => $messageText,
                "id" => $id,
        ),
     'notification' => array(
                "body" => "body of notification",
                "title" => "title for notification",
        )
    );
于 2019-01-13T07:59:52.510 回答