Flutter, Firebase ios, cloud message doesn't show when app in background, only if app in foreground

Viewed 975

I'm using flutter dependancies:

firebase_messaging: ^7.0.3
flutter_local_notifications: ^3.0.1+6

I send firebase cloud message like this:

{
"to": "/topics/demo_ios"
"notification" : {
"body" : "Hi there now now",
"title" : "Wow!",
"sound": "default"

},
 "priority": "high"
"data": {
    "title": "Hi there yes",
    "body": "Wow",
}
}

In ios the notification shows when the app is in the foreground, however when in the background it doesn't show at all. I tried reading and fixing many things, but is still doesn't work. I set background fetch, remote notificaitons, and background processing from xcode, and it still doesn't work.

I set FirebaseAppDelegateProxyEnabled to false in the plist file, but it didn't help.

I request permission:

_messaging.requestNotificationPermissions(
  const IosNotificationSettings(sound: true, badge: true, alert: true, provisional: false)
);
_messaging.onIosSettingsRegistered.listen((event) {print('2124: ios Setting registered');});
3 Answers

first I recommend you to update the plugin to firebase_messaging: ^8.0.0-dev.8, because iOS background handling is enabled there. See here this change:

  • iOS background handler support.

Next, how to integrate all of that to your flutter project, please follow official documentation FlutterFire (this is documentation just after 8.x.x-dev versions of the plugin.

Finally, please read more about APN, because Apple is handling differently those messages and you need to have the APNSConfig property inside your script for sending notifications.

If you are sending notifications via FirebaseAdmin, then please read this.

Do not forget, that in that case, you need to set content_available=True which is contained inside the APNSConfig property, so basically if you are using FirebaseAdmin you should include also property similar to this one:

apns=messaging.APNSConfig(
        payload=messaging.APNSPayload(
            aps=messaging.Aps(
                alert='alert text',
                sound='s',
                content_available=True,
                mutable_content=True,
                category='c',
                thread_id='t',
                custom_data={
                    'id': 'xxx',
                    'type': 'xxx',
                    'title': 'Robb',
                    'content': 'Axxxx',
                    'image': 'xxx',
                    'deepLink': 'xxxx',
                },
            ),
        )
    ),

I was also having the same problem ,try to upload IPA file on testflight then it work hope so.

Well, the solution was quite simple, and a mistake on my behalf.

As the documnents pub.dev state:

Generate the certificates required by Apple for receiving push notifications following this guide in the Firebase docs. You can skip the section titled "Create the Provisioning Profile".

Here is the link: https://firebase.google.com/docs/cloud-messaging/ios/certs

I forgot to do these steps. After doing this, it worked

Related