iosflutterfirebase-cloud-messaging

Alert + Data FCM message does not always trigger FirebaseMessaging.onMessage on iOS


I have a very simple flutter app which listens to firebase notifications.

The part listening is this:

FirebaseMessaging.onMessage.listen((RemoteMessage message) => print("hello"))

and the message being sent (from go) is this:

var apsMessage = messaging.Aps{
    ContentAvailable: true,
    MutableContent:  true,  // I think I don't need this
    Alert: &messaging.ApsAlert{
        Title: "Incoming message",
        Body:  "mybody",
    },
    CustomData: map[string]interface{}{
        "interruption-level": "active",
    },
    Sound: "default"
}

messagingClient.Send(ctx, &messaging.Message{
    Token: token,
    Android: ...,
    APNS: &messaging.APNSConfig{
        Headers: map[string]string{
            "apns-priority": "10"},
        Payload: &messaging.APNSPayload{
            Aps: &apsMessage,
            CustomData: map[string]interface{}{
                "data_key": "data_value",
            },
        },
    },
})

During the tests the app is always in the foreground, and I'm using a physical iPhone with the latest iOS 18.

The first message is being received correctly - I can see the notification popping and I can see the print in the MacOS console.

From the second message onwards I can only see the notification popping, but the print is never called.

It means that the code is technically correct, and does run correctly the first time, but subsequent notifications simply don't trigger the onMessage listener until some seemingly random time later when it does work. I don't seem to figure out what sequence of steps or how long it takes before it works again.

I saw that iOS throttles in case of exceptions but I don't see any exceptions on the onMessage callback.

Any reason iOS would simply choose not to call my callback?


Solution

  • iOS (APNs) background notifications are unreliable.

    Even if sent together with an alert, the background notification might simply be throttled and dropped.

    The only suggested way of solving it is by implementing a Notification Extension Service and manually synchronizing it with the dart code, eventually leading to mutable-content:1 designation on the FCM message. Requests handled by mutable-content:1 are considerably more reliable, and aren't throttled, as they're meant to invoke custom code on each notification.

    I am yet to implement one successfully. The current flutter firebase_messaging package does not support it yet, as implementing it well and within the runtime constraints of the environment is difficult.