4

I'm using the FCM Legacy HTTP API to send notifications to my React-Native Apps. When I send notifications with the "data" block filled out, I see all the data on my app. When I just populate the "notification" block however the message is empty when I receive it.

Is it possible to send true notifications through the API? Or can it only be used for data messages? I've been scouring through the documentation for a day now and Firebase seems to indicate it would send a real notification, some blog posts say only data goes out. In any case, I receive the message on the app in a call back when the app is open, but the title and text are missing. When the app is closed I don't get any notification sent to the system notification bar. I'm basing the structure on the firebase documentation here: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

Here is my code on the sending side (PHP):

$url = 'https://fcm.googleapis.com/fcm/send';
$data = array(
    "to"=>"/topics/myapp.notification",
    "notification" => array(
          "title"=>$title,
          "body"=>$body
    )
);

$options = array(
  'http' => array(
    'header'  => "Content-type: application/json\r\n".
                 "Authorization: key=" . $appInfo["server_key"] . "\r\n",
    'method'  => 'POST',
    'content' => json_encode($data)
   )
  );


$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

On the app side, I'm listening for the message when the app is open using:

this.messagePushListener = firebase.messaging().onMessage((message) => {
    console.log(JSON.stringify(message));
});

The message I receive is missing the title and the message:

{"_collapseKey":"com.xxx.myapp","_data":{},"_from":"/topics/myapp.notification","_messageId":"-M6IZKep_yvL4rxxxxx","_ttl":3600}
John P
  • 1,540
  • 2
  • 22
  • 34
  • did you set the data alongside with `.setTitle(notification.title)` `.setBody(notification.body)` so the final result will be: `.setTitle(notification.title)` `.setBody(notification.body)` `.setData(notification.data)` when you create a listener at `this.notificationListener = firebase.notifications().onNotification`? – Luiey Jun 18 '20 at 03:54
  • @JohnP did you find any solution? I am facing the same issue here. – Ayan Sengupta Apr 23 '21 at 00:07

0 Answers0