0
votes

I can send test notifications from Firebase console to my app both on iOS and Android. Therefore, my app is set up properly to receive push notifications on both platforms. However, when I use a cloud function to send notifications. Only, notifications on the Android device are received. No notifications show up on the iOS device. I suspect this may be related to the way that I create the payload in my cloud function. Maybe I am missing something for iOS. If you could give me some tips, that would be great.

I checked if the deviceToken for the iOS device is correct and it was correct.

I sent a test message using firebase console to the same deviceToken for the iOS device and the notification was delivered.

Therefore, I concluded my problem may arise from the cloud function I wrote. Thus, I share below the cloud function:

exports.notifToApp = functions.database.
ref(`/memInfo/{memId}/notifChoice/`).onWrite((snap, context) => {

//send only if exists and new notification OR if doesn't exist
if ((snap.before.exists() && (snap.after.val() !==  snap.before.val())) || !snap.before.exists()) {

//get notification body
const notificationTitle = snap.after.val().memName;
const notificationText = snap.after.val().notifText;

//get and loop over notification subscribers
return admin.database().ref(`/notifics/${context.params.memId}/notifSubs/`).once("value", subs => {
    if (subs.exists()) { 
    return subs.forEach(sub => {

//payload for notification
    const payload = {
        "notification":{
            "title": notificationTitle,
            "body": notificationText,
            "sound": "default",
            "click-action": "FCM_PLUGIN_ACTIVITY",
            "priority": "high"
        }
    }


//deliver notification
return admin.messaging().sendToDevice(sub.val().deviceToken, payload).catch(e => {console.log(e);});

    });
} else { //end: if returned any value
    return 0;
} 
});// end: get and loop over notification subscribers
} else { //end: send only if exists and new notification OR if doesn't exist
    return 0;
}
});

I do not get any error messages. Function completes successfully with status "OK."

I test using two devices: one android and one iOS. Both device tokens are saved correctly in the database for the cloud function to retrieve and use for sending messages.

I see the notification on the Android device running my app. I wish the notification to show up on the iOS device running the same app.

Test message notification sent from the firebase console shows up on both devices correctly.

1

1 Answers

0
votes

I realized that sendToDevice() used the legacy version of payload. I used send() in my function to use the newer version. (see answer: stackoverflow)

admin.messaging().send(payload).catch(e => console.log(e));

I changed the payload to include platform specific fields according to the latest guidelines (see firebase docs)

const payload = {
"token": sub.val().deviceToken,
"notification":{"title": notificationTitle,"body": notificationText},
"android": {"notification": {"sound": "default"}},
"apns": {"payload": {"aps": {"sound": "default"}}}
};

Now it works on both platforms.