flutter - Alert + Data FCM message does not always trigger FirebaseMessaging.onMessage on iOS - Stack Overflow

时间: 2025-01-06 admin 业界

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?