0

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?

hazelnutek
  • 173
  • 1
  • 14
  • Did you ever figure out if this was possible? I'm running into the same issue right now and would love to know if you found a solution. – Ben Smith Jan 11 '22 at 00:46
  • @BenSmith unfortunately no, I tried all the "universal" examples and ended up with mixed results between iOS and android. I ended up using a simple "gateway" service for sending notifications and it modified the payload depending on user's OS (I store the os together with the registered id). I still send bulk notifications with multiple IDs but eventually split them into 2 requests, one for iOS and one for android devices – hazelnutek Jan 11 '22 at 08:49

0 Answers0