-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[🐛] setBackgroundMessageHandler does not trigger on IOS #7988
Comments
Sorry, I was testing this last week and it worked. I'd advise stripping the app down to bare minimum for testing (i.e. strip out all but core + messaging dependencies), and firing messages via firebase admin. Example script: var admin = require('firebase-admin');
// 1. Download a service account key (JSON file) from your Firebase console and add to the example/scripts directory
// var serviceAccount = require('../ff-e2e-service.json');
var serviceAccount = require('../rnfb-service-account.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
// 2. Copy the token for your device that is printed in the console on app start (`flutter run`) for the FirebaseMessaging example
const androidToken = '';
const iosToken = '';
// 3. From your terminal, root to example/scripts directory & run `npm install`.
// 4. Run `npm run send-message` in the example/scripts directory and your app will receive messages in any state; foreground, background, terminated.
// If you find your messages have stopped arriving, it is extremely likely they are being throttled by the platform. iOS in particular
// are aggressive with their throttling policy.
admin
.messaging()
.send(
{
token: iosToken,
data: {
foo: 'bars',
float: '12034'
},
notification: {
title: 'A great title',
body: 'Great content',
},
android: {
// Required for background/terminated app state messages on Android
priority: 'high',
},
apns: {
payload: {
aps: {
// Required for background/terminated app state messages on iOS
contentAvailable: true,
},
},
},
},
)
.then((res) => {
if (res.failureCount) {
console.log('Failed', res.results[0].error);
} else {
console.log('Success');
}
})
.catch((err) => {
console.log('Error:', err);
}); This should help you at least confirm it works, before slowly adding further dependencies + logic to see what the issue is. |
@russellwheatley Hello, I've tried both expo(sdk 51) & rn cli(0.75.2) empty projects, only packages sent pushes by your example, I receive notification alert when app is backgrounded, but setBackgroundMessageHandler reproduction (no GoogleService-Info.plist in the repo) maybe there is something In the Firebase console/google cloud console/apple store connect I need to change to make it work? android works fine |
Tested it on a real device ios 15.8.1 and it works now I guess it doesn't work on new versions > 17ios like mentioned here |
@russellwheatley tested it on a real device 17.5.1 did a build via Xcode, at the first launch of the app, when I put it in background state, setBackgroundMessageHandler does work |
I have an app in production and my clients report the same problem, the app works fine in the foreground but when it goes to quit state it stops working, sometimes it works and sometimes it doesn't. There is a way to easily reproduce this error by simply restarting the iPhone. With this, you will no longer receive notifications in the background until you reinstall the app. |
current workaround is to add native NotificationServiceExtension (need to have mutableContent field for backend to make it work) |
does anyone know why when I send a test message on FCM, the notification DOES appear but my onBackgroundMessageHandler does NOT fire?? i'm just trying to test this log statement firing from a FCM Test message before figuring out why my server's notifications aren't firing
was trying to follow the specific instructions here: #7548 (comment) |
I'm still facing the same issue "@react-native-firebase/app": "^20.4.0", my payload in postman:
|
If you include a notification block in your FCM, the firebase-ios-sdk will post the notification directly to the user / notification center with no handlers called in your app ever |
If I remove a notification block in FCM, setBackgroundMessageHandler is still not working in the background/quit state tested it on a real device 17.4.1 did a build via Xcode, at the first launch of the app, when I put it in the background state, setBackgroundMessageHandler work More specifically, when I reinstall the app, it works in the first launch. But when I quit the app, sometimes it has to wait for about ~5 minutes to get a remote message from setBackgroundMessageHandler, and sometimes it is still not working How do you think I could do something to make BackgroundMessageHandler work? |
If you remove the notification block then you have delivery problems, yes. iOS - in my experience - is very picky about donating some of the user device's energy to your app to run it and call a handler in response to a data-only message. That is an iOS policy and there is not much you can do to impact it. For that reason, on iOS a notification block is encouraged so you have a chance at your FCM being delivered, but you'll need the user to interact with the notification for it to be useful. So the final answer is: you really must send FCM to the device that the user finds useful, the need to want to see that notification pop up and interact with it, because it's useful. If you don't, nothing you try to do will be high-percentage - you're fighting the iOS philosophy at that point and won't see any sort of reliable delivery. |
I've expanded my test to 4 devices:
on devices 2 & 3 & 4, it was still encountered in this case ↑ on device 1, it worked seamlessly although I rebooted the device or reinstalled my app lots of times. I think the issue occurred specifically on the ios 17 >= |
Which reads to me exactly like: "iOS, as it progresses from version to version, works even harder to preserve your battery so it lasts all day! Unlike Android where you are wondering if you'll make it through the day we work hard at Apple to make sure that your battery is used as efficiently as possible so worrying about charging is a thing of the past! Upgrade to the new iOS (or buy a new device that can run it) today!" Of course as a pesky application developer trying to implement features this is irritating because our code doesn't run But users love it |
It means that this library cannot support my case. So, from iOS 17, do I have to use another library to get notifications on background/quit state? |
@dbs-tuan you probably want to send a notification block, and use a NotificationExtensionHandler so you get a small time slice of power from the user device in order to do some handling before posting the notification to the user. Notifee.app can do that (it is a companion to react-native-firebase). But it is still limited, you do not get a lot of power / compute time to do work, just a tiny amount. Your use case may in fact simply not be technically possible depending on what exactly you want to do. The rules on iOS are that if the users cannot see it, you most likely simply cannot do it unless the user is actively interacting with the app. |
I'm using notifee only in case of increasing badge count. The notification template is generated from the API. My task is just simply to increase the badge count in the background/quit state. Additionally, my current test is local, I'm thinking of building my app to TestFlight and testing it again
So sad to hear that Thanks for your quick response. Maybe I'll try another way |
you should be able to - with notifee - send a notification block in the FCM to iOS and have your notification extension handler call the notifee API to increase the badge count prior to posting the notification to the user |
where could I get docs about "NotificationExtensionHandler" |
@Ga1atex Could you give me any tutorials or relevant documents related to "add native NotificationServiceExtension"? |
Issue
I use firebase and Notifee. I try to send data to my server, but
setBackgroundMessageHandler
does not work on iOS at all. On android this method works fine.I setup
remote-notification
,fetch
options in Xcode.content-available
is setted totrue
. Doesn't help.Project Files
Javascript
Click To Expand
App.tsx
:messaging.ts
:package.json
:firebase.json
for react-native-firebase v6:iOS
Click To Expand
ios/Podfile
:Environment
Click To Expand
react-native info
output:react-native-firebase
version you're using that has this issue:^20.4.0
Firebase
module(s) you're using that has the issue:@react-native-firebase/messaging
TypeScript
?Yes
&~5.3.3
The text was updated successfully, but these errors were encountered: