Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM notification title remains "FCM Message"

I'm trying to use the Firebase Cloud Messaging. I send the notifications from a Node.js server to my apps that are registered to the notification system.

My problem is that on Android 5.1 the notification is "FCM Message" even if I setted the title attribute in the nofitification json. It works fine in Android 6.0. I tried to reboot my device as well.

enter image description here

And this is the code I use to send the notification:

function sendNotificationToUser(userToken, message, onSuccess) {
  request({
    url: 'https://fcm.googleapis.com/fcm/send',
    method: 'POST',
    headers: {
      'Content-Type' :' application/json',
      'Authorization': 'key='+API_KEY
    },
    body: JSON.stringify({
      notification: {
        "title": 'My App Name',
        "body": message,
        "sound": 'default'
      },
      to : userToken
    })
  }, function(error, response, body) {
    if (error) { console.error(error); }
    else if (response.statusCode >= 400) { 
      console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    }
    else {
      onSuccess();
    }
  });
}

As you can see the notification title I send is "My App Name" but on device it shows "FCM Message".

What do I have to do?!

like image 775
Jackie Degl'Innocenti Avatar asked Dec 15 '22 02:12

Jackie Degl'Innocenti


2 Answers

You need to pass the title and then receive it in remoteMessage.getNotification().getTitle() , this will catch title and then display in the top or pass complete JSON from web and receive like this

JSONObject jsonObject = new JSONObject(remoteMessage.getData());

Here is the complete method:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // ...
    // TODO(developer): Handle FCM messages here.
    // Not getting messages here? See why this may be: https://firebase.google.com/support/faq/#fcm-android-background
    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
    }

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }

    // Also if you intend on generating your own notifications as a result of a received FCM
    // message, here is where that should be initiated. See sendNotification method below.
}

Ref link

like image 72
Decveen Avatar answered Dec 26 '22 13:12

Decveen


I found that it is a problem related to onMessageReceived callback.

enter image description here

As you can see on the receive a FCM guide

like image 26
Jackie Degl'Innocenti Avatar answered Dec 26 '22 12:12

Jackie Degl'Innocenti