EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

onMessageReceivedCallback behaves differently between platforms #600

Open 3rror404 opened 6 years ago

3rror404 commented 6 years ago

I'm having some problems getting notifications to work as I would like.

I'm using the following commands to test notifications:

Text and data notification:

curl -X POST --header "Authorization: key=**REMOVED**" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"badge\": \"1\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"content_available\": true, \"to\": \"**REMOVED**\"}" 

Data only notification:

curl -X POST --header "Authorization: key=**REMOVED**" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"**REMOVED**\"}"

Desired behaviour:

Actual behaviour:

iOS text-and-data:

iOS data-only:

Android text-and-data:

Android data-only;


I have a basic view-model that holds the init() code:

const firebase = require("nativescript-plugin-firebase");

exports.doInit = function() {
   firebase.init({
      onPushTokenReceivedCallback: token => {
          // you can use this token to send to your own backend server,
          // so you can send notifications to this specific device
          console.log("Firebase plugin received a push token: " + token);
      },
      onMessageReceivedCallback: message => {
          console.log("Title: " + message.title);
          console.log("Body: " + message.body);
          // if your server passed a custom property called 'foo', then do this:
          console.log("Value of 'foo': " + message.data.foo);
          console.log('foreground: ' + message.foreground);

          alert('You have a new alert. ' + message.body);
      }
    }).then(
        function (instance) {
          console.log("firebase.init done");
        },
        function (error) {
          console.log("firebase.init error: " + error);
        }
    );
  } else {
    console.log('Firebase already initialised.');
  }
}

I call doInit() from app.js. I've tried initialising in the first view rather than app.js but that doesn't seem to help.

Could anyone help me to achieve the desired behaviour?

manijak commented 6 years ago

I am having the same issue on iOS. Able to receive notifications normally when the app is in foreground, background and when suspended. But the two later states do not hit the onMessageReceivedCallback.

I have noticed that it has something to do with the notification-payload itself. It depends if you send a generic, hybrid or custom payload.

Generic-payload (used by the FCM console). It only contains the notification payload, and not the data payload. On iOS it works on all states, but hits the callback only when the app is in foreground.

{
 "to" : "TOKEN_FCM",
 "notification" : {
     "body" : "First Notification",
     "title": "Collapsing A"
 }
}

Hybrid-payload (used by the FCM console). Contains both the notification and data payload. On iOS it works on all states, but hits the callback only when the app is in foreground.

{
 "to" : "TOKEN_FCM",
 "notification" : {
     "body" : "First Notification",
     "title": "Collapsing A"
 },
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "data_id" : "30515",
     "data_type" : "article"
 }
}

Custom-payload (used by custom apps that use the FCM API, e.g. Postman). Contains only the data and not the notification payload. On iOS it hits the callback and returns the payload on all states. But does not show built-in banners/notifications because it is dependent on the notification payload. You receive the payload when one starts the app.

{
 "to" : "TOKEN_FCM",
 "data" : {
     "body" : "First Notification",
     "title": "Collapsing A",
     "data_id" : "30515",
     "data_type" : "article"
 }
}

I've not tested this on Android yet.

Lacos247 commented 6 years ago

It is very useless this way. Data-Notifications should be process even if the app is in background. It seems that the plugin does not support this? @EddyVerbruggen is this right?

Desired way to process push notifications when data message arrives (app is in background): The callback is triggered and the app can evaluate the incoming message and do some action - show a notification in the system tray with the plugin "local notifications" for example..

BR, Lacos

bozhidarc commented 6 years ago

My situation is similar to the one observed by @3rror404 under iOS, but different with that if the app is terminated and I tap on received push notification the onMessageReceivedCallback() is called. If the app is only in background it receives all messages as it is in foreground. If you try to resume the app by taping a push notification it doesn't call the onMessageReceivedCallback().

In my case I want to navigate to a certain page after the app is opened/resumed through a push notification.

curtiscarlson commented 4 years ago

@bozhidarc Did you ever solve this?

bozhidarc commented 4 years ago

@bozhidarc Did you ever solve this?

Sorry, but I have no recent experience with this functionality.