Zorgan
Zorgan

Reputation: 9123

Certain types of FCM data messages are not delivered to iOS device

I have a Cloud Functions environment that sends data and notification messages.

At the moment, I am testing FCM messages on an iPhone SE (2016) and an iPhone 7 Plus - and the behaviour is very inconsistent between the 2 devices and I'd like to know why.

The following cloud function sends a notification and data message - both of these successfully get delivered to both devices:

// These options are global for all my fcm messages
const options = { priority: "high", timeToLive: 30000, content_available: true }

function sendProfile() { 
    ...
    const fcmToken = ********
    const notif = {
        notification: {
            title: "test title",
            body: "test body"
        }
    }
    admin.messaging().sendToDevice(fcmToken, notif, options);
    const dataMsg = {
        data: {
            id: id,
            type: "match",
            uid: uid,
            name: name,
            age: age.toString(),
            bio: bio,
            img1: String(img1),
            img2: String(img2),
            img3: String(img3),
            pronoun: pronoun,
            error: String(bot)
        }
    }
    return admin.messaging().sendToDevice(fcmToken, dataMsg, options);
}

However for the following function:

  1. The notification message successfully gets delivered to both devices

  2. But the data message only gets delivered to the iPhone SE (not the iPhone 7 Plus)

     function sendPlace(fcmToken, placeSnapshot, matchName){
         let docId = placeSnapshot.id;
         let place = placeSnapshot.data();
         console.log("sendPlacee: ", place.name, " to: ", fcmToken);
         const dataMsg = {
             data: {
                 type: "place",
                 name: place.name,
                 latitude: place.l.latitude.toString(),
                 longitude: place.l.longitude.toString(),
                 instruction: String(place.instruction),
                 placeId: docId,
                 picture: String(place.picture1),
                 matchName: matchName,
                 address: place.address
             }
         }
         const notif = {
             notification: {
                 title: "test place function",
                 body: "test the body message"
             }
         }
         admin.messaging().sendToDevice(fcmToken, notif, options)
         return admin.messaging().sendToDevice(fcmToken, dataMsg, options)
     }
    

Only when I remove some of the payload, it successfully sends to the iPhone 7 Plus (I removed the instruction, picture and address key values from the data payload - and then it worked).

Any idea what the problem is here?

Edit: There are no problems with my Android devices.

Upvotes: 4

Views: 5140

Answers (2)

Andrea Gorrieri
Andrea Gorrieri

Reputation: 1772

The Firebase Cloud Messaging relies to the Apple Push Notification service (APNs) to send messages (up to 4KB in size) to the iOS app. In iOS, notifications and data messages are treated differently, in particular:

  • Notification message: FCM automatically displays the message to end-user devices on behalf of the client app. No callback is generated in this case on the app until the user taps on the notification opening the app (in this case the userNotificationCenter(_:didReceive:withCompletionHandler:) callback is triggered)
  • Data message: Data messages are converted by the FCM layer in the so-called background notifications ("content-available" : true). A background notification is a remote notification that doesn’t display an alert, play a sound, or badge your app’s icon, in this case client app is responsible for processing data messages. This kind of notification is designed to wake your app in the background giving it time to initiate downloads from your server and update its content. When a "data" ("background") notification is sent, the callback application(_:didReceiveRemoteNotification:fetchCompletionHandler:) is fired at the time the notification arrives and no sound or alert are triggered automatically. However, take into account this important note from the Apple docs:

The system treats background notifications as low priority: you can use them to refresh your app’s content, but the system doesn’t guarantee their delivery. In addition, the system may throttle the delivery of background notifications if the total number becomes excessive. The number of background notifications allowed by the system depends on current conditions, but don’t try to send more than two or three per hour.

So the problem could be related to the particular conditions of your phones: notifications messages are correctly delivered to both phones but, maybe, the iPhone 7 Plus os is throttling or delaying the reception of the data ones.

Upvotes: 8

Nandish
Nandish

Reputation: 1162

Check this blog if you can find some information on the FCM messaging and if you can figure out any issue relates to iPhone 7 plus device where you had to reduce the payload. https://firebase.googleblog.com/2019/02/life-of-a-message.html

Upvotes: 0

Related Questions