Search code examples
firebaseflutterbackgroundmessagingpayload

Flutter - Background notifications not working in iOS


I'm creating a public chat app in Flutter, and I can't receive background notifications on iOS, when I trigger the method admin.messaging().send(payload) from my Cloud Function.

Here's my payload in the Cloud Function :

var payload = {
  notification: {
    title: `My Title`,
    body: `My message`,
  },
  android: { priority: "high" },
  apns: {
    payload: {
      aps: {
        contentAvailable: true,
      },
    },
    headers: {
      "apns-push-type": "background",
      "apns-priority": "10",
      "apns-topic": "io.flutter.plugins.firebase.messaging", // bundle identifier
    },
  },
  topic: `mytopic`,
};

I tried a bunch of different payloads :

  • I tried notification + data
  • I tried only data (no notification) (in that case, I have to locally display the notification, but it is not working because the function FirebaseMessaging.onBackgroundMessage is never called)
  • I tried to change "apns-priority" to 5
  • I tried using a token instead of a topic

I set up carefully all I need to configure, thanks to the official documentation : https://firebase.flutter.dev/docs/messaging/usage/

I also checked Github issues, like these ones : 1041, 6112, 5988, 1644, 4300, 4097

What is working :

  • Background and foreground notifications on Android and iOS, when sent from Firebase Console (in 'Cloud Messaging' section)
  • Background and foreground notifications on Android only, when sent from Cloud Function
  • Foreground notifications on iOS, when sent from Cloud Function

What is NOT working :

  • Background notifications on iOS when sent from Cloud Function
  • When app is terminated, it does not work as well.

Solution

  • I finally resolved this issue ! 🎉

    I simply removed the line "apns-push-type": "background" in my payload. Now it is working.

    It appears that the line "apns-topic" was useless as well.

    Here is my final payload :

     var payload = {
        notification: {
          title: `# ${context.params.passion}`,
          body: `${newMsg["senderPseudo"]} ${
            type == "image" ? "a envoyé une image." : `: ${newMsg["message"]}`
          }`,
        },
        // Set Android priority to "high"
        android: {
          priority: "high",
        },
        // Add APNS (Apple) config
        apns: {
          payload: {
            aps: {
              contentAvailable: true,
            },
          },
          headers: {
            //"apns-push-type": "background", // This line prevents background notifications to be displayed
            "apns-priority": "10",
          },
        },
        token: "dnqTQVso60GfnnuOjHv8_e:APA91bElr-K3xkQMdYHX8VMrMZNCYCjO4zJlGseRh25AS_GT7cg9zlOGdQl4KXvr88ypeWjZjrPzrLRHitsQ-JKQK057ZQb_36c_lfsNjHXbYMYI2iS3jV_HGWf7Ene-ZlPvOb0aRr8u"
     };