Open
Description
Operating System
IOS 17.2.1
Browser Version
Safari/10.7.2
Firebase SDK Version
10.7.2
Firebase SDK Product:
Messaging
Describe your project's tooling
Vue, Vite
Describe the problem
I'm trying to receive push messages on Iphone pwa. The notifications are coming just fine, the problem is that the notification is showing even if the app is in the foreground.
On desktop (mac/windows) and android it's working fine, but on IOS the notification always shows up regardless if the app is open or not.
Steps and code to reproduce issue
importScripts('https://www.gstatic.com/firebasejs/10.7.2/firebase-app-compat.js')
importScripts('https://www.gstatic.com/firebasejs/10.7.2/firebase-messaging-compat.js')
const firebaseConfig = {} // my config
firebase.initializeApp(firebaseConfig)
const messaging = firebase.messaging()
// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// Keep in mind that FCM will still show notification messages automatically
// and you should use data messages for custom notifications.
// For more info see:
// https://firebase.google.com/docs/cloud-messaging/concept-options
messaging.onBackgroundMessage(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/img/logo.svg'
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
Activity
juyeongnoh commentedon Apr 20, 2024
Any updates? I'm facing the same issue.
dlarocque commentedon Aug 16, 2024
@pablor21 @juyeongnoh
Thanks for reporting this! Unfortunately, I haven't been able to reproduce it.
I have a PWA set up, and
onBackgroundMessage
running in afirebase-messaging-sw.js
only gets triggered when the app is running in the background.Could you share any additional information that may help me reproduce the issue? A minimal app or demo where this issue occurs would be really helpful
google-oss-bot commentedon Aug 23, 2024
Hey @pablor21. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.
If you have more information that will help us get to the bottom of this, just add a comment!
yergom commentedon Aug 27, 2024
Hi, I do have some new insights. The problem is that, under some circumstances that I have not been able to figure out, the service worker
self.client.matchAll
call returns always an empty list. Reproducing the issue is simple:But my tests are inconclusive. On iOS 16.7.5 clients are reported, on iOS 16.6.1 they are not. To get things worse, I have two separate iphones on ios 17.5, and one reports clients, the other one does not. This makes me think that there must be some OS level setting preventing the service worker from getting the correct client list.
This seems to be connected to this webkit bug: https://bugs.webkit.org/show_bug.cgi?id=268797. My feeling is that all the current iOS related bugs can be explained by it. Being able to get the client list is the base for sending foreground , reacting on notification clicks and changing the url of the background app, etc, etc
dlarocque commentedon Aug 27, 2024
How does this relate to the original issue? The reported issue is that
onBackgroundMessage
is being triggered when the app is in the foreground.yergom commentedon Aug 27, 2024
Let me elaborate a bit more if it was not clear. Have a look at the onPush listener:
firebase-js-sdk/packages/messaging/src/listeners/sw-listeners.ts
Line 72 in 4ff9474
Notice that onNotificationClicked has a similar issue
firebase-js-sdk/packages/messaging/src/listeners/sw-listeners.ts
Line 113 in 4ff9474
dlarocque commentedon Aug 27, 2024
Ah, I see- thanks for elaborating.
My understanding so far is that there is a WebKit bug that causes
self.clients
to be empty, which causes unexpected behaviour downstream when propagating the events throughonPush
andonNotificationClick
.I still don't quite understand how this is related to the WebKit bug you linked, where
notificationclick
events don't get fired in service workers. Could you elaborate on that?yergom commentedon Aug 27, 2024
@dlarocque If you read the comments on https://bugs.webkit.org/show_bug.cgi?id=268797 carefully, my conclusion is that the service worker used by the push notification system (APNs) is different than the one spawned by the page itself. Apparently, if the app is completely closed and is opened due to clicking on a push notification, then the APN's service worker will be linked to the WindowClient, and then the issue will not be present. I have not been able to reproduce this. However, I've tried the following:
On top of all of this, also be aware that currently webkit is also not implementing silent notifications correctly. This means that if you are lucky enough to get foreground notifications, the push subscription will be automatically unsubscribed after 3 foreground messages, as reported in #8010
My current solution is to overwrite firebase behavior's on ios, and make sure that I always create a notification, to avoid the sudden unsubscription, something along these lines:
bp-oleg commentedon Sep 2, 2024
I'm trying to achieve this behavior. PWA on iOS 17.6.1 does receive pushes via Firebase, shows notification while app is in background. But when app is in foreground, it receives messages in
onMessage
, then I need to show the notification, andnew Notification(title, options)
does not work. No idea how you get Firebase to show notifications in foregrounddlarocque commentedon Sep 6, 2024
This has been added to a list of known issues with FCM in iOS PWAs caused by WebKit bugs in our Wiki: https://github.com/firebase/firebase-js-sdk/wiki/Known-Issues
pablor21 commentedon Sep 8, 2024
I'm sorry! I was on vacation and I missed the whole discussion. It has been some time since I posted this issue and I don't remember how did I got the app working. It's good to know that people will be aware of this behavior and can plan for it accordingly.
leeyuchang commentedon Nov 21, 2024
Any progress?
bangluong commentedon Dec 25, 2024
Hi, how can you get notification on IOS? I add my website to home screen. after open app, it dont have popup ask permission.
my ios version is 17.2