onMessageReceivedCallback behaves differently between platforms
See original GitHub issueI’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:
- Created 6 years ago
- Comments:5
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.Hybrid-payload (used by the FCM console). Contains both the
notification
anddata
payload. On iOS it works on all states, but hits the callback only when the app is in foreground.Custom-payload (used by custom apps that use the FCM API, e.g. Postman). Contains only the
data
and not thenotification
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 thenotification
payload. You receive the payload when one starts the app.I’ve not tested this on Android yet.
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