28

We implemented push notifications using FCM in chrome and firefox and it worked fine, but Safari doesn't support Service Workers which is mandatory for FCM.

Did anyone deal with this kind of issues? any suggestions?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Nicolas Rojo
  • 703
  • 1
  • 7
  • 14
  • [Supported environments for the Firebase JavaScript SDK](https://i.stack.imgur.com/hjclf.png) https://firebase.google.com/docs/web/environments-js-sdk It seems that FCM does not support iOS yet. – G Justin Feb 07 '23 at 04:57

6 Answers6

20

While Service Workers now work on Safari, they are not enough.

The documentation says:

The FCM JavaScript API lets you receive notification messages in web apps running in browsers that support the Push API. This includes the browser versions listed in this support matrix.

So the ability to receive messages through FCM depends on the browser implementing the Web Push API, which Web Kit does not.

Safari is built on Web Kit, so doesn't support web push, so that means that Safari can't receive FCM notifications. All browsers on iOS devices are also built on Web Kit (as that is a requirement from Apple), so FCM won't be able to receive messages in any of them either

If you want to test whether the environment your code runs on can receive messages from FCM, you can use this snippet of code:

if (firebase.messaging.isSupported())
  ...
}

Update (2023-02-16): WebKit.org just announced support for Web Push in iOS/iPadOS 16.4 beta 1. We're investigating whether this also impacts Firebase Cloud Messaging's ability to deliver to devices with this version.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 3
    Is this still the case? According to the release notes for Safari 11.1, Service Workers are now supported. However, calling firebase.messaging().requestPermission throws an error: "messaging/unsupported-browser". Is there some other API support still missing? – Evan Kirkwood May 25 '18 at 22:34
  • 9
    Service Workers are supported in Safari 11.1 but it seems that it still doesn't support the Push API (https://caniuse.com/#feat=push-api). – Dimitrij Agal Jul 11 '18 at 08:33
  • 1
    At this moment, it seems that FCM is partially working (i tested with version 9.18 and 9.19, compat) on both iOS and Mac safari. The main issue is that in foreground, notifications are only received 3 times, after that, the firebase messaging token is revoked. This issue doesn't happen when app is in background and notifications are shown by the OS in the notification bar. Is anyone else having this behavior? – Andrei F Apr 19 '23 at 08:39
  • 1
    Hey Frank, do you have an update on this? Would FCM be able to support push notifications on IOS Safari? Andrei, do you mind posting an answer with more explanation on how you got it working? I would appreciate it very much! – cecunami Apr 20 '23 at 18:05
12

Safari still does not support Web Push API, thus Firebase Cloud Messaging service.

Here is supported browsers in Firebase: https://firebase.google.com/support/guides/environments_js-sdk#browsers

I'd suggest using .isSupported() instead of other solutions.

if (firebase.messaging.isSupported())
    const messaging = firebase.messaging();
}

See the documentation for details on .isSupported().

o4ned
  • 140
  • 1
  • 7
  • In my case, I needed a version lock at 8.2.9 per https://github.com/firebase/firebase-js-sdk/issues/4633 – Isaiah Jul 11 '21 at 08:17
4

I'm not sure about this new Safari and Service Workers situation, but I tried to implement FCM push-notifications in my iOS app and had to experience that you need an APN (Apple-Push-Notification) certificate first. I'm not sure if this is needed if you're working with Safari, but I could imagine that Apple also wants it's push notifications first to be redirected to the APN servers.

Matt Harris
  • 177
  • 9
  • 2
    Yes, to work with push notifications on Safari (desktop) we need the APN certificates generated for Web Push notifications from Apple Dev. console. – KayKay Apr 25 '18 at 14:57
4

Apple says that Safari 16 (macos) should now support Push API but I didn't manage to make push API notifications work correctly. I tried firebase messaging web v9 (which should be a wrapper over Push API from what I understand) and it seems to work in background (when delivered by the service worker) but in foreground only receives notifications 3 times, then it stops working.

Andrei F
  • 4,205
  • 9
  • 35
  • 66
1

Here is how I got Safari push notifications to work on IOS 16.4.1 and firebase 9.19.1.

  1. Implement the generic notification code and make sure it works on a normal device (not IOS).
  2. (Edit - This step doesn't appear to be necessary even though it's mentioned in the documentation) Make sure you call "getToken" on a specific user action like clicking on a button. Otherwise, IOS will not allow it.
  3. Make sure you add a manifest.json file that is accessible on the root of your app at "https://yourapp/manifest.json". If you are using angular, you can add a route in the angular.json assets section to expose the file. Example manifest.json:

{
  "name": "test",
  "short_name": "test",
  "description": "test",
  "display": "standalone",
  "theme_color": "#ffffff",
  "icons": [
    {
      "src": "/assets/logo.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "/assets/logo.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}
  1. Update your IOS to 16.4+
  2. Enable notifications in Safari. Go to Settings -> Safari -> Advanced -> Experimental Features. Enable both "Notifications" and "Push Notifications".
  3. Open your web app on IOS Safari. Click on the share icon and "Add to Home Screen".
  4. Close the browser and open the app from the home screen shortcut.
  5. Click on the button that will call "getToken". A popup will be displayed from IOS that will ask the user for notification permissions.

Hope that helps!

cecunami
  • 957
  • 5
  • 12
0

FCM doesn't support Safari browser.

If you want to integrate PUSH notification for Safari browser, follow this link. Safari has its own mechanism for it.

https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/NotificationProgrammingGuideForWebsites/PushNotifications/PushNotifications.html#//apple_ref/doc/uid/TP40013225-CH3-SW1