Skip to content

[firebase_messaging]: iOS background message not invoked when app is terminated and user switches to different app afterward #9625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
iosephmagno opened this issue Sep 27, 2022 · 7 comments
Labels
platform: ios Issues / PRs which are specifically for iOS. plugin: messaging resolution: user This was a user issue, e.g. invalid configuration or code. type: enhancement New feature or request

Comments

@iosephmagno
Copy link

iosephmagno commented Sep 27, 2022

Is there an existing issue for this?

  • I have searched the existing issues.

Are you aware of the differences between iOS and Android background message handling?

  • I understand that iOS and Android background messages behave differently, and I've designed my application with that in mind.

Do you have an active Apple Developer account?

  • I have an active Apple Developer account.

Are you using a physical iOS device to test background messages?

  • I am using a physical iOS device to test background messages.

Have you enabled "Remote Notifications" & "Background Mode" (Checking options for "Background Processing" & "Remote Notifications") in your app's Xcode project?

Yes, pic below.

Screenshot 2022-09-27 at 12 52 00

Have you created an APNs key in your Apple Developer account & uploaded this APNs key to your Firebase console?

Yes, pic below.

Screenshot 2022-09-27 at 12 54 33

Have you disabled method swizzling for Firebase in your app?

Yes, pic below.

Screenshot 2022-09-27 at 12 56 17

Are you sending messages to your app from the Firebase Admin SDK?

Yes, our code below:

admin
.messaging()
.send({
data: {...data, notificationType: 'chat'},
token: deviceToken,
android: {
priority: 'high',
data, // without it, badge is not displayed in foreground
},
apns: {
payload: {
aps: {
contentAvailable: true,
},
},
headers: {
'apns-push-type': 'background',
'apns-priority': '5', // Must be 5 when contentAvailable is set to true.
'apns-topic': 'com.backend.presence', // bundle identifier
},
},
})

Have you requested permission from the user to receive notifications?

  • I have the relevant permission to receive notifications.

Have you used the 'Console' application on your macOS device to check if the iOS device's system is throttling your background messages?

From Console, we see that data-only notifications are always cancelled if app is terminated and user opens a different app.
"CANCELED: com.apple.pushLaunch.com.backend.presence:A680C5 at prioritv 5 !" (pic below).

As we wrote in #9300, our comment here #9300 (comment) ,
Apple told us that silent notifications on iOS are not meant to be used as on Android and we should use "notification" , which are never throttled and arrive quickly. Ofc, since most time content is encrypted, Apple provides the possibility to modify content (decrypt it) before displaying the badge. As mentioned, this is done by using "mutable-content:1" + a Service App Extension.

Screenshot 2022-09-27 at 15 28 57

Additional context and comments

Hello and thanks for the investigation!

Our understanding is that firebase_messaging should integrate "notification" with "mutable-content:1" and "Service App Extension". Without this feature, it is not possible to use the plugin for apps that send high priority encrypted notifications (eg. financial or trading apps, chat and social media apps, banks apps). In these cases, app might send encrypted content that should be decrypted before displaying the badge.

Why we cannot use "data-only notification" as is it now?
Beyond the throttling, they don't work at all when "app is terminated and user opens another app". This is not a bug of firebase_messaging but Apple's policy.

Why we cannot use "notification" as it is now?
Because without mutable-content and Service App Extension, badges will be displayed before having the opportunity to decrypt notifications' content.

Hope this will be helpful.

@iosephmagno iosephmagno added Needs Attention This issue needs maintainer attention. platform: ios Issues / PRs which are specifically for iOS. plugin: messaging type: bug Something isn't working labels Sep 27, 2022
@iosephmagno
Copy link
Author

iosephmagno commented Sep 27, 2022

We just noticed that OneSignal provides the feature. Please see Step 3 of follow documentation:
https://documentation.onesignal.com/docs/flutter-sdk-setup

Is it possible to have something like that also in Firebase_Messaging?
That would be much appreciated.

@hatemragab
Copy link

@iosephmagno
Is one signal notifications extension
Will let the ios background handler to be invoked for ios?

@iosephmagno
Copy link
Author

iosephmagno commented Sep 28, 2022

@hatemragab we are investigating. The idea is, if we get to modify remote notification content before diplaying headup on iOS, we can use Redis on the server to map users’ devices so that:

  • To android devices, we will send data-only notifications.
  • To ios devices, we will send remote notifications with mutable-content:1

As of now, it seems that this is the only way to deal with notifications when content decryption and 100% delivery is required on iOS.

If firebase_messaging adds a Notification Extension, above idea will require minimal code on our side. Otherwise, we must use a different plugin on iOS and use firebase_messaging on android only.

@darshankawar darshankawar added the triage Issue is currently being triaged. label Sep 28, 2022
@darshankawar
Copy link

Thanks for the detailed report. Based on the details provided and also additional context provided, I am keeping this issue open and treating as a potential enhancement and to know team's insights on it.

/cc @russellwheatley

@darshankawar darshankawar added type: enhancement New feature or request and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. labels Sep 28, 2022
@ben-xD
Copy link

ben-xD commented Oct 3, 2022

I took a look since @iosephmagno emailed me about this issue.

Some challenges to support this include:

  • launching the Flutter application (or a separate Dart entrypoint) when message is received by notification extension (the Flutter app doesn't always exist when it is received), to allow users to write Dart to e.g. decrypt messages.
  • send message (UNNotificationContent) from Notification extension to iOS host app (e.g. using MMWormhole), then send a message from iOS host app to Flutter app (Method channels), to allow the user to decrypt the message (or mutate the notification in any way they want) and send the modified notification back Flutter, to iOS host, then to app extension.

@awaik
Copy link

awaik commented Nov 8, 2022

Hi!
Are there is any news about this issue? For us, it is a blocking thing and we are going to switch to direct APN from the Firebase.

@iosephmagno
Copy link
Author

@awaik unfortunately you must use iosNotificationExtension. At least, by doing so you will have ios Communication Notification layout on your app.

I'm closing this issue on my side. Maybe explaining this issue in official doc might help devs. We wasted weeks only to understand it.

To Recap:

Does your app send clear data and you don't need ios Communication Notification layout on iphone?
Then use firebase_messaging with remote notification. Notifications will work on all app states.

Does your app send encrypted data or you need ios Communication Notification layout on iphone?
Then use iosNotificationExtension and remote notification. The Extension will decrypt field and display badge on background state. On foreground state, Extension will decrypt field and firebase_messaging handler will be triggered.

Never use silent notifications on ios because they are throttled and cancelled most of time. This is why they don't work when app is terminated, it is not firebase_messaging issue, it is os that cancels them.

Keep using silent notification on Android. This means duplicate data in your notification json (one for android using silent notification and one for ios using remote notification + mutableContent:1)

@darshankawar darshankawar added the resolution: user This was a user issue, e.g. invalid configuration or code. label Nov 9, 2022
@firebase firebase locked and limited conversation to collaborators Dec 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform: ios Issues / PRs which are specifically for iOS. plugin: messaging resolution: user This was a user issue, e.g. invalid configuration or code. type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants
@awaik @ben-xD @hatemragab @darshankawar @iosephmagno and others