Closed mikeroneer closed 2 years ago
@AymanMubark Hi, my customer is using the Firebase Console to send messages. Do you know how to set content_available to true in Firebase Console? Does your app's onBackgroundMessage get called when you send a message from the Firebase Console? ( I am testing in a debugging environment.)
You can try adding this here
@UsamaKarim Thanks! I've tried it many times but it didn't work... I tried both content-available: true and _contentavailable: true.
I would like to know if anyone has successfully activated onBackgroundMessage in the following environment(or similar)
in my case it doesn't work even if I put content_available: true.
Yea i just add the default code on documentation and add notifications future on xcode
I don't think it's possible, I am facing the same problem @UsamaKarim. I believe we are getting this error. https://stackoverflow.com/questions/67697194/display-flutter-notifications-even-when-application-has-being-killed
It would be nice if the firebase console allow us to edit the apns. If you decide on a server side solution the API doesn't allow you to segment the audience :(
If someone else knows how to edit in the firebase console, it would be nice!
same issue for me
Worked for me
{
"notification": {
"title": "Message Title",
"body": "Message Body",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"priority": "high",
"sound": "blank.caf",
"content_available": true
},
"data": {
"type": "Oka",
"title": "This is title",
"message": "This is message"
},
"apns": {
"payload": {
"aps": {
"content-available": true,
"sound": "default"
}
}
},
"to": "fR------------"
}
Hey @mikeroneer, I've just tested this with a message containing data + notification & just data. It worked in both instances. Please provide a small reproducible repo for me to check. You must have a problem in you setup.
@russellwheatley when you tested this, did you have "contentAvailable" set to true?
Any news about this issue? I don't know if it matters but it happens to me on iPadOS 15.5
This is my current setup: https://stackoverflow.com/questions/72583264/firebase-messaging-wont-call-backgroundhandler-on-ios-11-4-1
@helenaford Yes, here is an example payload using a POST request. Here if you wish to use nodejs admin SDK.
EDIT2: I removed previous comment. I was wrong, check next answer to see how background handler should work.
TLDR: I've tested again with the example project and my own project and I can confirm that background handler is working properly when iOS app is on background OR terminated. If you want to know how, keep reading.
In my case, initializing FirebaseMessaging.onBackgroundMessage(_myBackgroundMessageHandler);
so late was preventing the background handler from working on terminated state since it was taking too long to register the handler.
Previously, I was waiting for requestPermission()
to finish before declaring the stream listeners and moving it earlier (on main) made it work. Anyways, I've realized that there are some setups that can stop your implementation from working, so, if you really want to make it working, follow these steps and you should be good.
Tested with
Hey @mikeroneer, I've been debugging this issue and I've come to the realisation that messaging works best when the manual installation step is taken for setting up iOS (i.e.
GoogleService-Info.plist
file added to the Runner in your Xcode project).
FirebaseMessaging
listeners as soon as possible. Ideally just right after Firebase.initializeApp()
and ALWAYS before runApp()
. I have dependency injection and it's now working even if I wait for the dependencies to be injected, but make sure it's done before the runApp()
. For further clarification, you have my current implementation at the end of this comment in case is of use if some of you have a similar implementation.This has been mentioned aswell, but just make sure that when you send your payload, you set content_available: true
. There is some confusion about the spelling of this parameter since it changes in different languages. So, to make it clear:
NodeJS Admin SDK:
{
token: "your_token",
notification: {
title:"FCM Message",
body:"This is a FCM notification message!",
},
data: {},
apns: {
headers: {
'apns-priority': '10'
},
payload: {
aps: {
contentAvailable: true,
}
}
}
}
REST
{
"message":{
"token":"your_token",
"data":{},
"notification":{
"title":"FCM Message",
"body":"This is a FCM notification message!",
},
"content_available": true,
"priority": "high"
}
}
FCMRepositoryImpl
Hi, can you share this test project on GitHub? Did you add anything to the AppDelegate?
@tal412 Sorry, this is a private commercial project, I can't. What else do you need exactly? Maybe I can clarify whatever you need.
Regarding AppDelegate, I've added the following lines for local_notifications plugin, but I checked that it's not related to background handler functionality:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
}
I have just used the Console app to debug my iPad and check whether the notification is even received by the device - and it does.
This is what I get:
default 20:48:06.069282+0300 SpringBoard Received incoming message on topic com.matkonit at priority 5
default 20:48:06.082948+0300 SpringBoard [com.matkonit] Received remote notification request 3823-43DB [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background]
default 20:48:06.083005+0300 SpringBoard [com.matkonit] Process delivery of push notification 3823-43DB
Setting "badge" : 1 will actually show the badge and yet, Firebase won't call the handler... Any ideas?
Also, has anyone tried it on the latest version of iPadOS? I'm currently testing on an iPad with 15.5 installed.
i actually receive the notification even when the app is terminated. But the second one ! the first one is always missed, gone then after the second one i always receive them
Are you still able yo receive data message on background when app is terminated?
@helenaford Yes, here is an example payload using a POST request. Here if you wish to use nodejs admin SDK.
@russellwheatley My customer is using the Firebase console to send messages. Have you tested that it works correctly using the Firebase console?
@tal412 Does this work for silent notifications ( data only)?? While app is terminated
I have just used the Console app to debug my iPad and check whether the notification is even received by the device - and it does. This is what I get:
default 20:48:06.069282+0300 SpringBoard Received incoming message on topic com.matkonit at priority 5 default 20:48:06.082948+0300 SpringBoard [com.matkonit] Received remote notification request 3823-43DB [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background] default 20:48:06.083005+0300 SpringBoard [com.matkonit] Process delivery of push notification 3823-43DB
Setting "badge" : 1 will actually show the badge and yet, Firebase won't call the handler... Any ideas? Also, has anyone tried it on the latest version of iPadOS? I'm currently testing on an iPad with 15.5 installed.
@tal412 Does this work for silent notifications ( data only)?? While app is terminated
Yes
@tal412 do you use data dict only on the server? Or you add both (notifications and data)?
@tal412 do you use data dict only on the server? Or you add both (notifications and data)?
Data only, but adding the notification tag brings the same result.
Hello, guys. I was able to send/receive and handle data and notification with the following message structure using FCM v1 REST API:
{
"message": {
"token": "device-token-goes-here",
"notification": {
"title": "Notification global title",
"body": "Notification global body"
},
"data": {
"title": "Data global title",
"body": "Data global body"
},
"android": {
"notification": {
"title": "Notification title Android",
"body": "Notification body Android"
},
"data": {
"title": "Data title Android",
"body": "Data body Android"
}
},
"apns": {
"payload": {
"aps": {
"alert": {
"title": "Data title iOS",
"body": "Data body iOS"
},
"content-available": 1
}
},
"headers": {
"apns-priority": "5"
}
}
}
}
@tal412 did you try to use it for a while?
Because mine works fine but for some minutes then it turns off automatically, which is weird... Like there's no exact duration, maybe if I sent like 10 data messages then suddenly it stop receiving the messages, and sometimes even 40 or 50 messages
Is there some sort of limitations?
Hello, guys. I was able to send/receive and handle data and notification with the following message structure using FCM v1 REST API:
{ "message": { "token": "device-token-goes-here", "notification": { "title": "Notification global title", "body": "Notification global body" }, "data": { "title": "Data global title", "body": "Data global body" }, "android": { "notification": { "title": "Notification title Android", "body": "Notification body Android" }, "data": { "title": "Data title Android", "body": "Data body Android" } }, "apns": { "payload": { "aps": { "alert": { "title": "Data title iOS", "body": "Data body iOS" }, "content-available": 1 } }, "headers": { "apns-priority": "5" } } } }
@devkaio does it work without showing notifications
@devkaio does it work without showing notifications
As far as I tested, it was working if put only apns payload. Additionally, I added the following code on Info.plist
<key>GoogleUtilitiesAppDelegateProxyEnabled</key>
<false/>
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
@devkaio @tal412 same result here, It works fine foreground/background and then when terminated , it just work for some minutes then stops forever....
@devkaio does it work without showing notifications
As far as I tested, it was working if put only apns payload. Additionally, I added the following code on Info.plist
<key>GoogleUtilitiesAppDelegateProxyEnabled</key> <false/> <key>FirebaseAppDelegateProxyEnabled</key> <false/>
Did it work before you added those lines?
@tal412 for me yes, they made no difference
<key>GoogleUtilitiesAppDelegateProxyEnabled</key>
<false/>
@markusaksli-nc This solves problem. Data-only notifications get delivered and background handler starts working.
<key>GoogleUtilitiesAppDelegateProxyEnabled</key> <false/>
@markusaksli-nc This solves problem. Data-only notifications get delivered and background handler starts working.
@AlexanderFarkas can you test after your swipeup your app (kill it),for howlong will data messages keep arriving?
@rulefahd It depends on your device. But it's by design - if you swipe up, your app will no longer receive data messages and your background workers won't wake up.
From apple docs:
In most cases, the system does not relaunch apps after they are force quit by the user. One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system. When password protection is enabled on the device, the system does not launch an app in the background before the user first unlocks the device.
@AlexanderFarkas but how do other apps update their app in background while it's killed, like whatsapp when you click on message, you can navigate to the chat, which requires some background work and data from the message?..
@rulefahd WhatsApp is a VoIP application, so it uses PushKit (which is more powerful) instead of UserNotifications. And PushKit notifications wake device up even if app is closed.
@AlexanderFarkas I read that apple policy state that you can only send data messages through voip if its related to it , and you can't send normal data message through it,
And after some research I found they use service extension for notifications, but im not yet familiar if I can use it to active a background function in my main app or just through native swift, ( I'm on flutter)
Edit : it doesn't active background... Unfortunately
<key>GoogleUtilitiesAppDelegateProxyEnabled</key> <false/>
@markusaksli-nc This solves problem. Data-only notifications get delivered and background handler starts working.
This fixed the problem but only temporarily. After a couple hours the notifications just won't arrive again. Even after few days (while the app is in the background and not terminated). Nothing...
It's hard to tell whether it's a bug in iOS or in the Firebase implementation.
any update on this issue?
@tal412
Dumb ios limitation..
@tal412
Dumb ios limitation..
When implementing this logic myself via native notification service I didn't have this issue...
When implementing this logic myself via native notification service I didn't have this issue...
When using native notification services, will sending messages from the Firebase console work correctly on iOS? I need to know if Firebase Console is useless for sending messages to iOS.
When implementing this logic myself via native notification service I didn't have this issue...
When using native notification services, will sending messages from the Firebase console work correctly on iOS? I need to know if Firebase Console is useless for sending messages to iOS.
In my case it's pretty useless right now.
@devkaio @tal412 same result here, It works fine foreground/background and then when terminated , it just work for some minutes then stops forever....
I tried this with the app killed and even after device restart. Added "apns-push-type": "alert",
at headers.
{
"message": {
"token": "device-token",
"data": {
"customKey": "customValue"
},
"android": {
"data": {
"title": "Data title Android",
"body": "Data body Android"
}
},
"apns": {
"payload": {
"aps": {
"alert": {
"title": "Data title iOS",
"body": "Data body iOS"
},
"content-available": 1,
}
},
"headers": {
"apns-push-type": "alert",
"apns-priority": "5"
}
}
}
}
I'm facing the same issue and have been stuck for days, I've tried using manual installation instead of dart initialization for firebase. I've tried disabling GoogleUtilitiesAppDelegateProxyEnabled and FirebaseAppDelegateProxyEnabled together and separately and yet the app does not call the background function, nevertheless, the documentation mentioned not to disable method swizzling.
This is the payload I'm sending from the server
"data": {
"data" : "data"
},
"apns": {
"payload": {
"header" : {
"apns-push-type": "background",
"apns-priority": "5"
},
"aps": {
"content-available" : 1
}
}
}
I've tried content-available alongside mutable-content, I've tried changing apns-push-type from background to alert and indeed it does reflect the physical device I'm testing on by showing the correct payload that was sent when capturing the springboard process log
default 21:03:05.828270+0200 SpringBoard [com.package.package] Received remote notification request C0EF-C43D [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background]
I've tried embedding the badge number and it does change that when providing it.
I've to note that marking GoogleUtilitiesAppDelegateProxyEnabled and FirebaseAppDelegateProxyEnabled as disabled lets the app receives silent notifications in the terminated state for a while, but after restarting the device the issue is still there, and the console is spiting new log with the following message.
default 21:13:36.562361+0200 dasd com.apple.pushLaunch.com.package.package:29FB01:[
{name: ApplicationPolicy, policyWeight: 50.000, response: {Decision: Absolutely Must Not Proceed, Score: 0.00, Rationale: [{[pushDisallowed]: Required:0.00, Observed:1.00},]}}
], FinalDecision: Absolutely Must Not Proceed}
I'm using firebase_core: ^1.18.0 with firebase_messaging: ^11.4.2 on flutter 3.0.2
If someone could give me any insight that would be much appreciated. It's really frustrating since it works as intended on Android.
@elsystm your payload is not right. The structure should be with headers (in plural) as a child of apns, and contentAvailable is cammelCase. You can check the message reference
{
"data": {
"data" : "data"
},
"apns": {
"headers" : {
"apns-push-type": "background",
"apns-priority": "5"
},
"payload": {
"aps": {
"contentAvailable" : true
}
}
}
}
@jorgeroncero Thanks for your input, I've changed the location of the headers, but unfortunately using contentAvailable in camelcase results in the following error using firebase HTTP send method
https://content-fcm.googleapis.com/v1/projects/projectname-projectId/messages:send
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "INVALID_ARGUMENT"
},
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "message.token",
"description": "Invalid registration token"
}
]
}
]
I'm using the following documentation to test notifications with different payloads using Postman for rapid testing
https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send
And, as mentioned above the OS receives the right headers that were sent using the HTTP send method.
default 21:03:05.828270+0200 SpringBoard [com.package.package] Received remote notification request C0EF-C43D [ waking: 0, hasAlertContent: 0, hasSound: 0 hasBadge: 0 hasContentAvailable: 1 hasMutableContent: 0 pushType: Background]
@devkaio Thanks for your input, I've tried yes and this resulted in unwanted behavior.
The background function which is defined in dart was never called/executed and the OS has displayed the title and body which are defined the in the fcm request, we are using silent push notifications on iOS to let the background function handle showing the notification using flutter local notification instead of intercepting the push notification natively and handling it using Notification content extension.
With firebase_messaging plugin 11.4.2 The Android app receives silent messages in open, minimized, and fully closed states, sending the user's geolocation.
IOS app receive silent messages and perform necessary actions in opened and collapsed state only with firebase_messaging 11.4.1 plugin.
Maybe there is some version of the plugin that works in a completely closed IOS application?
I send a message: POST https://fcm.googleapis.com/fcm/send
{ "to" : "/topics/alldriver", "priority": "high", "content_available": true, "data": { "data" : "data" } }
@elsystm we are attempting to implement notifications in a similar fashion: letting the FlutterLocalNotificationsPlugin in the app do the work after receiving a GCM data message instructing it to do so. This works really well on Android, but the background handler does not get called on IOS for us either. Sending an SNS notification message through AWS works fine on IOS, that comes through and displays, but not data messages. I guess I don;t have anything to add except that we are facing the same issue. My message as output by AWS:
{"GCM": "{\"data\": {\"title\": \"yeehaw\", \"message\": \"not working\", \"create\": \"test\"}, \"android\": {\"priority\": \"high\"}, \"apns\": {\"payload\": {\"aps\": {\"contentAvailable\": \"true\"}}, \"headers\": {\"apns-priority\": \"5\", \"apns-topic\": \"io.flutter.plugins.firebase.messaging\"}}}"}
Maybe someone has experience using only Apple (APN). Does a silent message come in and does your logic run from such a message?
Bug report
Describe the bug According to https://firebase.flutter.dev/docs/messaging/usage/#message-types, the background handler should be invoked for notification messages, data messages or a combination of both when the app is in the background or terminated. In our project, it is not invoked in any of those cases. For the sake of completeness it is worth to mention that the
onMessage
stream fires properly when the app is in foreground and also the notification handled by the FCM-SDK is shown when the app is in background/terminated. It's just theonBackgroundMessage
which is never invoked.We are aware of the
content_available
flag, however, the issue does not just relate to silent (data only) messages.Steps to reproduce
Steps to reproduce the behavior:
_firebaseMessagingBackgroundHandler
Expected behavior
onBackgroundMessage
is invoked when the app is in background or terminated.Sample project
Edit: Payload (suggested by @markusaksli-nc)
We have tried quite a lot of different payload combinations, with both, containing a data object only and a notification object respectively. We have also tried addressing the recipient via a topic (which is our intended use case) as well as with the unique device token. Background modes (Background fetch and Remote Notifications) are enabled too. On the server side, we are using the Java SDK, however, we have also tried sending a POST request directly via Google's OAuth 2.0 Playground.
The following indicates a sample payload:
Additional context
firebase_messaging: ^10.0.0
Flutter doctor
Run
flutter doctor
and paste the output below:Click To Expand
``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.0.3, on macOS 11.3 20E232 darwin-x64, locale en-AT) [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3) [✓] Xcode - develop for iOS and macOS [✓] Chrome - develop for the web [✓] Android Studio (version 4.1) [✓] IntelliJ IDEA Ultimate Edition (version 2020.3.1) [✓] VS Code (version 1.56.2) [✓] Connected device (3 available) ```Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
``` Dart SDK 2.12.2 Flutter SDK 2.0.3 dependencies: - firebase_analytics 8.1.0 [firebase_analytics_platform_interface firebase_analytics_web firebase_core flutter meta] - firebase_core 1.2.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.0.4 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 10.0.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] ... transitive dependencies: - firebase 9.0.1 [http http_parser js] - firebase_analytics_platform_interface 2.0.1 [flutter meta] - firebase_analytics_web 0.3.0+1 [firebase firebase_analytics_platform_interface flutter flutter_web_plugins meta] - firebase_core_platform_interface 4.0.1 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.1.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.0.4 [collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 3.0.0 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.0.0 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] ... ```