I'm using cordova-plugin-push
to send push notifications via FCM service.
My notifications need to be "heads up" so they pop up on the user's screen while in background.
As for now, I have only managed to achieve this on Android by sending a data-only payload. Normal notifications like this one:
{
"registration_ids":[],
"notification": {
"title":"Test",
"body":"Test body",
"sound":"default",
"android_channel_id":"PushPluginChannel"
}
}
are behaving kinda 'silent' on android, they don't wake up the device (tried adding priority:high
with no success) despite the channel being configured with importance: 5
.
And in case of older devices (Android 7, which doesn't support channels) the notification doesn't pop up and only silently appears in the notification bar.
This is fixed by stripping the notification object and sending only data (as soon as I add notification, it stops working):
{
"registration_ids":[],
"data": {
"title":"Test",
"body":"Test body"
}
}
This way, notifications perfectly wake the device's screen and pop up on both Android 7 and Android 10.
BUT, now iOS doesn't show this notification at all (all configuration is correct, APNS certificates are good, permissions are assigned). I have read that this is normal on iOS, data notifications (content_available: true
) are only meant to trigger the app's background worker to fetch data.
I have also found answers to send only data for android in order for heads up to work, for example here: https://stackoverflow.com/a/38455693
If that's the case, is there any way to send a single FCM payload and make it work on both iOS and Android? I want to send one FCM notification and pass multiple registration tokens, not worrying about their platforms.
iOS needs to have the notification object, whereas android cannot. I tried doing something like this:
{
"registration_ids":[],
"data": {
"title":"Test",
"body":"Test body"
},
"apns": {
"aps": {
"payload": {
"notification": {
"title": "Test",
"body": "Test"
},
"alert": {
"title": "Test",
"body": "Test"
}
}
}
}
}
to include the notification for iOS only, but it still didn't work. Is there any way, or do I need to send different payloads for iOS and android?