Skip to content

Commit 0b3db49

Browse files
authored
docs(messaging): Note about keeping sw up to date (#2553)
Add warning missmatch firebase version could lead unable to get notification
1 parent 6e4c62d commit 0b3db49

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/messaging/messaging.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ You can either use the `firebase-messaging-sw.js` file provided in the docs or y
5050
],
5151
```
5252

53+
[Warning] Remember update the `firebase-messaging-sw.js` everytime you update the `firebase` in package.json. The missmatch version could lead to unable to receive notification in `foreground`, you can create your `firebase-messaging-sw.js` like this:
54+
55+
```js
56+
// Give the service worker access to Firebase Messaging.
57+
// Note that you can only use Firebase Messaging here, other Firebase libraries
58+
// are not available in the service worker.
59+
importScripts('https://www.gstatic.com/firebasejs/[the number of version matching with firebase in package.json]/firebase-app.js');
60+
importScripts('https://www.gstatic.com/firebasejs/[for example: 7.16.1]/firebase-messaging.js');
61+
62+
// Initialize the Firebase app in the service worker by passing in the
63+
// messagingSenderId.
64+
65+
firebase.initializeApp({
66+
apiKey: '<your-key>',
67+
authDomain: '<your-project-authdomain>',
68+
databaseURL: '<your-database-URL>',
69+
projectId: '<your-project-id>',
70+
storageBucket: '<your-storage-bucket>',
71+
messagingSenderId: '<your-messaging-sender-id>'
72+
});
73+
74+
// Retrieve an instance of Firebase Messaging so that it can handle background
75+
// messages.
76+
const messaging = firebase.messaging();
77+
78+
79+
```
80+
5381
### Requesting permission
5482

5583
Once you have the Firebase Messaging Service Worker set up and installed, you need to request permission to send a user notifications. While the browser will popup a UI for you, it is highly recommend to ask the user for permission with a custom UI and only ask when it makes sense. If you blindly ask for permission, you have an extremely high chance of getting denied or blocked.

0 commit comments

Comments
 (0)