1

GCM was working fine but now fcm do not recive any notification if it was sent by server it only work if o send from firebase console I am almost sure the problem is in array form i have in app:

        String messageBody = remoteMessage.getNotification().getBody();

and in php:

        $fields = array(
        'registration_ids'  => $this->devices,
        'data'              => array( "message" => $message ),
    );

what do you think please

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
code ketty
  • 194
  • 2
  • 12
  • is the FCM app using the same Project / Sender-ID as GCM version of your app? You can either migrate the old project to a "firebase project" from the firebase console, or register the FCM app for a secondary sender via FirebaseInstanceId.getInstance().getToken("sender2", "FCM") also the data-payload of the message is available in remoteMessage.getData() not remoteMessage.getNotification().getBody(). The second field is a for the "notification" payload. – Diego Giorgini Jun 03 '16 at 17:11
  • yes its not a sender-Id issue, so it should be getData() only or getData("message")? whith GCM I used getString("message") and work fime – code ketty Jun 03 '16 at 17:39
  • getData() will return a Map object where you can access the key/values you set in the data payload. In your case you should use getData().get("message") https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage.html – Diego Giorgini Jun 03 '16 at 17:44
  • aha nice, but then what about the notifications that comes from the firebase console would the app be able to recive without .getNotification().getBody();? – code ketty Jun 03 '16 at 20:20
  • The notifications that come from the console can only be a display-message. Which means that if the app is in background it will automatically display a notification-message. If the app is in foreground the onMessageReceive() method will be invoked and you can read the content of the notification via remoteMessage.getNotification().get* PS: while you can only send display-messages, those messages can include a "data" payload. See the "advanced options" in the Web Console. Do these comments address the original question? If so I will move them to an answer. – Diego Giorgini Jun 03 '16 at 21:49
  • can you ask your question obviously? – Edalat Feizi Jun 04 '16 at 08:15
  • @Diego actually yes its very valuable informations about notifications system. rewrite it as an answer so i can accept it – code ketty Jun 04 '16 at 08:19
  • @Edalat the problem is setting the the right message parser code for that server code – code ketty Jun 04 '16 at 08:21
  • this could be help you http://stackoverflow.com/questions/37371990/how-can-i-send-a-firebase-cloud-messaging-notification-without-use-the-firebase?rq=1 – Edalat Feizi Jun 04 '16 at 11:07

1 Answers1

2

In your question you are sending a data-message, which is a message with a payload like: {"registration_ids" : ["token1"], "data" : { "key1" : "value1"}}

To receive the data payload (in the example: key1:value1), the client app should to use:

String value1 = remoteMessage.getData().get("key1");

FCM also supports display-messages.
Example: {"registration_ids" : ["token1"], "notification" : { "body" : "hello"}}
These messages are automatically displayed when the app is in background. And they call onMessageReceived() only if the app is already open and in foreground. In this second case you can access the field of the original notification via: remoteMessage.getNotification().getBody()

Display messages can also include a {"data" : { "key1" : "value1"}}.
If the app is in foreground it can access these values like a data-message.
If the app in bakground you need to wait until the user click the notification and starts an activity.
At that point you can access the data via: getIntent().getExtras()

PS: the notifications sent via Firebase Web Console are only display-messages.

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50