question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onMessageReceivedCallback behaves differently between platforms

See original GitHub issue

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:

  • If the app is in the foreground, onMessageReceivedCallback will be called when either notification type is received.
  • If the app is in the background or terminated, onMessageReceivedCallback will be called when the app is opened.

Actual behaviour:

iOS text-and-data:

  • If the app is in the foreground the notification is received, onMessageReceivedCallback is called and I am able to access any data attached to the notification (✔︎).
  • If the app is in the background or terminated the notifications are received but when the notification is tapped to open the app onMessageReceivedCallback is not called (✖️).

iOS data-only:

  • If the app is in the foreground onMessageReceivedCallback is called immediately (✔︎).
  • If the app is in the background or terminated onMessageReceivedCallback is called when the app is opened and I am able to access any data attached to the notification (✔︎).

Android text-and-data:

  • If the app is in the foreground the notification is received, onMessageReceivedCallback is called and I am able to access any data attached to the notification (✔︎).
  • If the app is in the background or terminated the notifications are received. When the notification tapped to open the app onMessageReceivedCallback is called but I am not able to access any data attached to the notification (✖️).

Android data-only;

  • If the app is in the foreground onMessageReceivedCallback is called immediately (✔︎).
  • If the app is in the background onMessageReceivedCallback is called immediately (✖️ - but i can live with this behaviour).
  • If the app is terminated, onMessageReceivedCallback is not called when the app is opened (✖️).

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?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
manijakcommented, Jan 31, 2018

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.

1reaction
Lacos247commented, Mar 19, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Solved]-`flex` appearing differently on mobile device to desktop
I solved this issue by changing the flex container's display attribute to block , then doing the same for its children: .flex, .hero,...
Read more >
NativeScript Firebase onMessageReceivedCallback does not ...
I am using this firebase plugin and I followed everything in the docs apart from one thing that is Enabling push support in...
Read more >
Tutorial / Realtime Peer to Peer Demo with Vue.js
In this demo, we're going to build a simple app that will allow one of the peers to elect themselves the “leader”, and...
Read more >
A Fun Peer-to-peer Drawing Game Built with Vue.js - Morioh
During scoring, each progression from starting caption through drawings and ... scale between a library and a framework depending on different use cases....
Read more >
Envato Tuts+ Code - Mobile Development - RSSing.com
The view model owns the model, and it acts as a proxy to the view controller. ... The weather data could come from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found