Mohammed Turky
Mohammed Turky

Reputation: 67

getNotification().getBody() Fails

I am trying to extract the message from google firebase notification message.

here is my code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

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

the remoteMessage.getFrom() returns the correct value while remoteMessage.getNotification().getBody() crashes with the following log:

.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)

any idea how to fix it ?

Upvotes: 3

Views: 5821

Answers (6)

pruthwiraj.kadam
pruthwiraj.kadam

Reputation: 1093

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

Upvotes: 0

Jayakumar
Jayakumar

Reputation: 323

sendNotification(remoteMessage.getNotification().getBody()); 
  • this is for Developer console default push notification.

You should use

sendNotification(remoteMessage.getData().get("message"));
  • this is for custom push from any server, "message" denoted by server side.

Upvotes: 4

Raman B
Raman B

Reputation: 339

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

Upvotes: 1

Tarun Umath
Tarun Umath

Reputation: 910

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

This will work, if a notification come to server side not console.

Upvotes: 0

Diego Giorgini
Diego Giorgini

Reputation: 12717

To expand on my comment posted under the question:

remoteMessage.getNotification().getBody() is only available if you are sending a notification-message. Either using the firebase console or the server-api with
payload = { "notification" : { "body" : "my body"} }

if you are sending a data-message like { "data" : { "mykey" : "myvalue"} } then on the client you should use the method: remoteMessage.getData()

Upvotes: 10

Mohammed Turky
Mohammed Turky

Reputation: 67

The answer is: (By @DiegoGiorgini):

getBody is only available if you are setting the option notification: { body: "my body"} when you send the message. If you are sending a data payload you should be using the method getData()

Upvotes: 0

Related Questions