【问题标题】:getNotification().getBody() FailsgetNotification().getBody() 失败
【发布时间】:2016-05-29 17:32:07
【问题描述】:

我正在尝试从 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)

知道如何解决吗?

【问题讨论】:

  • 你得到一个 NullPointerException - 可能 remoteMessagenullremoteMessage.getNotification() 返回 null 并因此调用 .getBody() 引发 NullPointerException
  • @ishmaelMakitla 不为空,因为它返回“来自”值
  • 但这就是您的错误日志所说的:java.lang.NullPointerException at com.example.mturky.testfirebase.MyFirebaseMessagingService.onMessageReceived(MyFirebaseMessagingService.java:31) - 请检查具体行。
  • @ishmaelMakitla 是的,我知道,这就是为什么我不明白问题出在哪里
  • 也许您应该为remoteMessage.getNotification() 添加一条日志语句并查看打印出来的内容 - 我怀疑这可能是 NULL。

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


【解决方案1】:

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

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

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

【讨论】:

  • 有用..!!你能告诉我如何只显示从“remoteMessage.getData()”方法获取的数据主体。目前我在我的应用程序中收到整个有效负载作为通知,如下所示:{time=01-08-2016 12:35:22, message=demo message}
【解决方案2】:
sendNotification(remoteMessage.getNotification().getBody()); 
  • 这是用于开发者控制台默认推送通知。

你应该使用

sendNotification(remoteMessage.getData().get("message"));
  • 这是用于从任何服务器进行自定义推送,“消息”由服务器端表示。

【讨论】:

  • 为我工作..非常感谢:) : ) :)
【解决方案3】:

您的 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"));

【讨论】:

    【解决方案4】:

    答案是:(@DiegoGiorgini):

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

    【讨论】:

      【解决方案5】:
        showNotification(remoteMessage.getData().put("","message"));
      

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

      【讨论】:

      • showNotification(remoteMessage.getData().put("","message"));
      【解决方案6】:
       $message = array(
              'registration_ids' => $registrationIDs,
              'data' => array(
                      "message" => $messageText,
                      "id" => $id,
              ),
           'notification' => array(
                      "body" => "body of notification",
                      "title" => "title for notification",
              )
          );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 2020-10-07
        相关资源
        最近更新 更多