-
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
messaging.setBackgroundMessageHandler: How to invoke when app is in quit state on iOS #5656
Comments
I love it, with the exception of react-native-splash-screen there - save yourself future pain crazycodeboy/react-native-splash-screen#289 (comment), use https://github.com/zoontek/react-native-bootsplash :-) If you want to hit the edit button on the tips-n-tricks page to share this more permanently, I think it would be a good add |
@mikehardy To make sure we are on the same page, you are talking about this tips page on the documentation site where I can submit an edit? |
@waqas-syed yes! |
This is awesome. Solved my issue facing for almost 3 days now. Thanks! :) |
Hi @waqas-syed , I tried the method and same configuration by sending notifications through firebase admin SDK. I got it working in foreground and background, but I had no success getting it working when the app is quit state. I just get the remote notification and not any data when in quit state. |
Hey @Aexonic-Abhishek , are you getting the data when the app is in quit state, I tried firing up a local notification but it didn't work out for me well. Can you please share what approach has worked for you? |
@saahiil-pny
messaging().setBackgroundMessageHandler(async remoteMessage => { AppRegistry.registerComponent(appName, () => HeadlessCheck);
export default AppFake; Note: I have created a new file(AppFake.js) for returning null. It wasnt working for me when I was directly returning the null in index.js file. |
This is specifically for iOS. This doesn't work when app is in quit/killed state. Can you please confirm that this works when app is quit/killed state? |
@saahiil-pny Yes, this solution works specifically for when app is in quit/killed state in iOS. |
Just wanted to drop by and say this suggestion is absolutely awesome. Spent last couple of days on this and was scratching my head, but this works perfectly. |
I also have a similar problem in ios here are some terms I would like to define
The was I test if the app calls setBackgroundMessageHandler is that I append the notification in an array in async-storage using const message = { does not work for quit state however, this works const message = { note:
however, the problem I have is that
|
Hmm - you should definitely carefully watch the logs from the phone to see what they say about message receipt and delivery, iOS logs are pretty noisy when watched from Console.app (or Xcode, a bit less noisy) but they may have some clues. |
@barnamumtyan I'm also facing the same issue like you.
I got this error in firebase function logs : Could you advice plz? |
@waqas-syed @saahiil-pny @mikehardy any thoughts? This is following the documentation and suggestions above. Messages are sent through FCM console and payloads are not modified. I'm getting a similar issue to the above, however, it is not triggering the setBackgroundMessageHandler() method when receiving a notification in background state. I can receive notifications fine, it is just that the handler is not called when trying to add headless for iOS in background/quit state. I've tried to follow the approach suggested in the documentation above, however, it hasn't worked. Are there any suggestions of triggering the messaging().setBackgroundMessageHandler function like previously? messaging().setBackgroundMessageHandler(async (remoteMessage) => { function getIfHeadless() { if (Platform.OS === 'android') { AppRegistry.registerComponent(appName, () => getIfHeadless); const AppFake = (): null => { export default AppFake; |
A similar problem, I registered everything in the same way as described above, but nothing works, help const Wrapper = ({ isHeadless }) => {
if (isHeadless) {
console.log("Headless");
return <AppFake />;
}
return (<App />)
};
AppRegistry.registerComponent(appName, () => Wrapper);
AppFake:
const AppFake = () => {
return null;
}
export default AppFake; AppDelegate:
|
what could be the problem? |
@NayChiLynn are you sending the request yourself using fetch, or using a rest cilent like postman? OR are you using the firebase library (on your server) like in https://rnfirebase.io/messaging/usage#main? In case 1 as I mentioned in my earlier post, I think this is the most straightforward way In case 2, using the firebase library on the server, which is very confusing |
@Wimmind based on your other post, you are sending incorrect FCM JSON and I would not expect anything to work. |
Hey @mikehardy any suggestions on this? Just can't seem to understand what the issue is
|
Great idea for the solution guys! I was just wondering: with proposed solution, what happens if the user opens the app after it was awaken with the Fake prop? How would we need to manage this case? If I'm understanding this right, the code inside the isHeadless block will be called and the fake prop will be instantiated. And finally, I looked at this blog post https://www.fullstacklabs.co/blog/tracking-push-notification-status-react-native and saw that to trigger some code when app is in killed state on iOS, we must use the Notification app extension. Are we sure it's possible to run some code without it on iOS? 🤔 Thanks a lot for your time 🙏 |
@louisencontre this may be the reason Invertase has invested so much time and effort in creating....a notification package with a notification service extension https://notifee.app/react-native/docs/ios/remote-notification-support :-) |
Ah yes thank you @mikehardy 🙏 I got to check out Notifee, it looks really nice 👌 |
I speak here only in reference to Fcm json contents, running on ios. Data block? Background handler might run. Unreliable by design Mixed data and notification blocks? No handlers called until users interaction on displayed notification, unless you have an extension Notification block only? Same as above. |
Ok thank you very much @mikehardy very clear 👌 |
But you do not specify versions of anything, you do not include the JSON you send via the FCM REST API to do your testing, you do not include the sources of documentation you've consulted already, what you've tried, what your device is for testing, app state and how you achieved it, and you include no evidence from the device indicating what happened, so no help may be offered. https://stackoverflow.com/help/how-to-ask |
I am using fcm testing tool online not rest api. |
@rohail111 then you are not fully in control of your tests since you cannot know the FCM JSON contents. I suggest you rethink your test strategy and use FCM REST API, I wouldn't say that as such a strong suggestion except it's actually really easy to use. Then you have full JSON control. Worth it in my opinion. |
@mikehardy if possible, can you share proper JSON data object by which we can trigger the backgroundMessageHandler event in case of FCM REST api. |
@rohail111 there are quite a few examples in the repo Here's one #2997 (comment) |
I was wasting tons of time trying to fix my app, and the solution was so dumb I thought had to share it here: I forgot to enable background app refresh in the iOS settings screen. setBackgroundMessageHandler will not work without this option enabled |
@lucasmartinsd same happened to me too. Enabled background app refresh and it works. On another issue, seems like
|
@mikehardy I didn't find any mention of iOS background app refresh OFF in the document. Would you like me to submit a PR to add that in? Also just wondering is that scenario being covered anywhere? I found one issue (see my comment above) and wondering is that flow covered / raised by anyone previously. Edit |
@roti-c137 I think it is documented here? https://rnfirebase.io/messaging/usage/ios-setup#enable-background-modes |
That is enabling background mode in Xcode. For iOS users, they have the ability to toggle background app refresh in their device’s setting as below. Also it is disabled by default if user is on low power mode. The implication is end user / during actual device testing, user has the ability to switch background mode off. I myself was scratching my head for a few days due to this 😅 |
Oh yes of course sorry. If you could make a PR to the docs that might help, there's an edit link on each page. I think I use device info to check status in code |
just faced this, needed the content-available flag, even if background app refresh is on |
Can you post all of your json object.
…On Fri, Jun 10, 2022, 12:44 AM Helena Ford ***@***.***> wrote:
just faced this, needed the content-available flag, even if background app
refresh is on
—
Reply to this email directly, view it on GitHub
<#5656 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AN7W3UTP46MGJBCETDZE3R3VOJCRHANCNFSM5C6DHOCA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
@waqas-syed Thanks for sharing this, I have try to get this working for a full day, after using your code it worked 👍 For anyone else looking for the solution, make sure to modify AppDelegate.m .
|
Thanks a lot!! SplashScreen.hide(); I don't know why, but for me this was necessary. Without it, the app would launch silently, but the |
And I am experiencing another problem in my environment. Start the app → Close the app. But if a few minutes pass after closing the app, it stops working. Specifically, I am using react-native-callkeep in Does anyone else have a similar problem? |
Yes Am also facing issue Specially in IOS |
It is expected behavior on iOS that you will not receive data-only FCM in the "killed" state (that is: you have quit the app, and waited some time, so the operating system kills the app - that is what iOS does after "quit") If you want reliable delivery of FCM on iOS you must include a notification block in your FCM. |
This solution just saved my day (for Android surprisingly enough). |
I am having the same problem, can you give me the solution? |
Try this payload ,worked for me. |
I followed what you said above but Platform IOS isHeadless is always equal to undefined |
u got setBackgroundMessageHandler error also? |
@santaclose666 |
i'm already follow all fcm doc and using more notifee/react-native. notification in background and quit mode is still popup but console.log() in setBackgroundMessageHandler or any method handler background or quit mode is not trigger current version react-native: 0.72.3 my code here: messaging().setBackgroundMessageHandler(async remoteMessage => { notifee.onBackgroundEvent(async ({type, detail}) => { console.log('background app', notification); if (type === EventType.ACTION_PRESS && pressAction.id === 'mark-as-read') { function HeadlessCheck({isHeadless}) { return ; AppRegistry.registerComponent(appName, () => HeadlessCheck); |
This worked for me too! Background refresh was ON so I was wondering what else could be wrong. Also worth noting is that I had added the properties before, but not inside the notification object. |
Documentation Feedback
This is a solution for invoking
messaging().setBackgroundMessageHandler()
when the app is in quit state on iOS.In the past couple of days, I have gone through a ton of issues on the same topic: #3530, #5057, #4104, #3367 just to name a few.
I was able to receive notifications in the foreground, background and also when the app was in quit state. Handlers worked in every scenario too, except for one.
My only problem was: setBackgroundMessageHandler not being invoked when the app was in quit state, even though I was receiving the notification. I also tried to send data-only messages but the result was the same. Even the 8 second delay wasn't working for me.
I was even able to invoke the app in a headless state using
content-available: 1
in the message'sapns
section.Then I realised that the
setBackgroundMessageHandler
was invoked if I mount theApp
component instead of returningnull
when the app was invoked in the headless state. So I started digging what exactly was required in theApp
component and to my surprise, it only required a component to be mounted, even when the component contained nothing and rendered nothing.So to sum up what I did:
Index.js
The
AppFake
component simply contains this:It's doing nothing, apart from hiding the
SplashScreen
that I am using, I found that this was essential in my case.This is the message payload:
This worked in my case, although I am not sure whether this will work for everyone. Just sharing it so anyone else can test and see if it gets their background handler working as well.
I have tested this on version
12.7.3
, but the scenario was the same on version10.8.1
, so it should probably work on that too.React Native Firebase
andInvertase
on Twitter for updates on the library.The text was updated successfully, but these errors were encountered: