-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 never fired #71
Comments
@wizzar how did you solve this? I have the same problem... |
Turns out the problem was in my json. There's a footnote on the documentation that says if your json defines notification property, then the setBackgroundMessageHandler is sort of ignored. "Note: If you set notification fields in your HTTP or XMPP send request, those values take precedence over any values specified in the service worker." So, this won't trigger the handler:
but this will:
This way you can send the notification in the data. |
Thank you so much! |
I had the same problem, but not due to the "notification" field. This was NOT working (despite the lack of "notification"):
This is working:
Sounds like a bug, since according to the FCM doc this "content_available" flag is only for iOS... |
I am wondering why the included notification should take precedence over the service worker... The problem is that this "feature" ruins my efforts to use the same messages for different platforms (eg. Web and Android), since I do not want to populate the "click_action" field in the sent message (as it would break the click handling in Android), but I still want to send the user to a page when the notification is clicked on the web. It would be easy to achieve this if I could eg. include the desired target url in a special field of the data payload and use the service worker to inject it as a click_action in the generated notification... |
@nemphys I have been having the exact same problem. It seems like a catch 22. You must include the "notification" object for IOS to display a notification when the app is not in the foreground. If you include the "notification" object then you cannot customize the "click_action" in a Web app. If you specify a "click_action" as a URL then Android will not open your app when it is clicked (IOS opens your app fine surprisingly). If you are not worried about IOS then you could implement the FirebaseMessagingService in your Android app and remove the "notification" object. You could then use custom data keys and control the code to show the notification yourself in both the Web app and the Android app. Doing this would cause IOS not to show any notifications when your app is not in the foreground though. I completely agree with you, they should allow you to override the notification code (to set a custom "click_action") even when a "notification"object is specified. Or they should allow for an "android_click_action" which overrides the "click_action" for Android apps. |
@UniBrowse Nice to know that I am not alone here :-) PS. I have a feeling that I will go through all the trouble to create a separate message logic server-side for the web app and after a few days they will finally implement it... |
+1 |
How can you cutomize the click_action? My messages are sent without the notification property. I tried to add the click_action property to the notificationOptions in my BackgroundMessageHandler like this:
But nothing happens when I click on the notification. I read somewhere, that this is not supported. So created an Event Listener, that reacts to a click on the push notification, something like this:
(I got this from here: https://developer.mozilla.org/en-US/docs/Web/API/WindowClient) By trying this, the clientList is empty. So by clicking on the notification it opens a new window even if the tab, where my application is running, is still open. Anyone have an idea how to fix this? |
@nemphys this is a known limitation at the moment and something that I've raised as an issue with the FCM team. |
@gauntface where do we track progress on this "catch 22" api? |
@Birowsky, This is what I was told when I asked the Firebase team for a solution for the meantime and how I would know if they had changed anything in the future. _
_ |
@kaziller you should accept clients in a service worker like this: https://developer.mozilla.org/en-US/docs/Web/API/Clients |
Okay, this is what we call 'GAMBIARRA' in Brazil, (or, workaround for fancy people). It works in Chrome, and Firefox, didn't tested in other browsers... Make sure to call this BEFORE Then, you can access the 'notification' data as '_notification' inside the handler /*
* We need a generic class to hold data and methods, that inherit from Event
*/
class CustomPushEvent extends Event {
constructor(data) {
super('push')
Object.assign(this, data)
this.custom = true
}
}
/*
* Overrides push notification data, to avoid having 'notification' key and firebase blocking
* the message handler from being called
*/
self.addEventListener('push', (e) => {
// Skip if event is our own custom event
if (e.custom) return;
// Kep old event data to override
let oldData = e.data
// Create a new event to dispatch
let newEvent = new CustomPushEvent({
data: {
json() {
let newData = oldData.json()
newData._notification = newData.notification
delete newData.notification
return newData
},
},
waitUntil: e.waitUntil.bind(e),
})
// Stop event propagation
e.stopImmediatePropagation()
// Dispatch the new wrapped event
dispatchEvent(newEvent)
}) |
This should be better documented. It's not intuitive and definitely a gotcha. |
I converted @ivanseidel solution (btw thank you) to old good javascript, and I'm posting here to help someone who needs it:
|
@marcosbrigante your solution works but causes the notification to display 4 times (original, new, original, new) any idea why that is? |
@gauntface Is there any progress on resolving this issue? Specifically, on resolving the fact that |
To open an existing tab this worked for me :
|
can you help me on get this working please |
@marcosbrigante I am seeing that your solution works but causes the notification to display 2 times: original & new |
This was very helpful thank you |
I used ivanseidel's code, and I was facing the issue of duplicating notifications. I did some tinkering with the code and now it works as expected. Hope this helps everyone.
|
Wonderful, I had the audacity to modify a little and create a repository to collaborate with the evolution of this gambiarra! https://github.com/BrunoS3D/firebase-messaging-sw.js https://github.com/BrunoS3D/firebase-messaging-sw.js/blob/main/firebase-messaging-sw.js |
If you are using react native and @react-native-firebase/messaging.
|
could you provide me your index.js (hide your necessary part after that send me here it will very helpfull for me) because i am using RNVoip call with fcm so in my backgroundhandler it will not working properly (like ringtone are not playing and some point to navigate to anthor screen when app is in kill mode) |
@TitanArmy Right now, I'm not using this callback in my code it's there so that I don't get any error. Currently the implementation is something like this: |
Which json ? And how did u disable caching in js ? |
The background handler is not working: no matter what I do, the sw always use the regular onMessage, displaying the actual notification I sent instead of the custom one set on the handler.
My code is the exact same as the one in the repo:
The text was updated successfully, but these errors were encountered: