Skip to content

Commit 12eee6c

Browse files
authored
Merge pull request #56 from lupas/simplifyFirebaseMessagingInit
Simplify firebase messaging init
2 parents 39b99ea + 6415271 commit 12eee6c

File tree

5 files changed

+103
-3
lines changed

5 files changed

+103
-3
lines changed

docs/getting-started/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ modules: [
6565
'welcome_message': 'Welcome'
6666
}
6767
},
68-
initAuth: null
68+
// Experimental Features - use with caution:
69+
initAuth: null,
70+
initMessaging: false
6971
}
7072
]
7173
],

docs/options/README.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ You can customize how remoteConfig should be initialized with the following sett
108108
## initAuth (EXPERIMENTAL)
109109

110110
::: warning
111-
This feature has not been fully tested for all cases, use it with care. It might get changed completely in future updates. If you have any issues with the initAuth feature please let us know [here](https://github.com/lupas/nuxt-fire/issues/53) and help us improve it.
111+
EXPERIMENTAL FEATURE: This feature has not been fully tested for all cases, use it with care. It might get changed completely in future updates. Please only use it for test purposes. If you have any issues with the initAuth feature please let us know [here](https://github.com/lupas/nuxt-fire/issues/53) and help us improve it.
112112
:::
113113

114114
Set up SSR-ready onAuthStateChanged() without any effort.
@@ -131,3 +131,26 @@ When onAuthStateChanged() gets triggered by Firebase, the mutations/actions defi
131131

132132
**onErrorMutation & onErrorAction:**
133133
(error)
134+
135+
## initMessaging (EXPERIMENTAL)
136+
137+
::: warning
138+
EXPERIMENTAL FEATURE: This feature has not been fully tested for all cases, use it with care. It might get changed completely in future updates. Please only use it for test purposes. If you have any issues with the initAuth feature please let us know [here](https://github.com/lupas/nuxt-fire/issues) and help us improve it.
139+
:::
140+
141+
Set up Firebase Messaging without any boilerplate code.
142+
143+
```js
144+
initMessaging: true
145+
```
146+
147+
Setting the \__initMessaging_ flag to true automatically creates a service worker called `firebase-messaging-sw.js` i nyour static folder. The service worker is fully configured for FCM with the newest Firebase scripts.
148+
149+
The option does so far:
150+
151+
- Create a service worker.
152+
153+
Planned features:
154+
155+
- Create a plugin that initializes messaging & listeners.
156+
- Maybe add helper functions that make the implementation as easy as possible.

index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,21 @@ export default function nuxtFire(moduleOptions) {
7474
)
7575
}
7676

77+
// Add Messaging Service Worker
78+
if (options.initMessaging) {
79+
this.addTemplate({
80+
src: path.resolve(__dirname, 'templates/firebase-messaging-sw.js'),
81+
fileName: path.resolve(
82+
this.options.dir.static,
83+
'firebase-messaging-sw.js'
84+
),
85+
options: {
86+
firebaseVersion: '7.3.0',
87+
messagingSenderId: options.config[options.currentEnv].messagingSenderId
88+
}
89+
})
90+
}
91+
7792
// Add Helper File
7893
this.addTemplate({
7994
src: path.resolve(__dirname, 'helpers/index.js'),

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nuxt-fire",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"license": "MIT",
55
"description": "Intergrate Firebase into your Nuxt project.",
66
"main": "index.js",

templates/firebase-messaging-sw.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const options = <%= serialize(options) %>
2+
const version = options.firebaseVersion
3+
const messagingSenderId = options.messagingSenderId
4+
5+
// Get ENV && set messagingSenderId
6+
if (this.location.hostname === 'localhost') {
7+
importScripts(
8+
'https://www.gstatic.com/firebasejs/' + version + '/firebase-app.js'
9+
)
10+
importScripts(
11+
'https://www.gstatic.com/firebasejs/' + version + '/firebase-messaging.js'
12+
)
13+
firebase.initializeApp({
14+
messagingSenderId: messagingSenderId
15+
})
16+
} else {
17+
// Only works on Firebase hosting!
18+
// other option, add PRD messagingSenderId = '337088779183' and do the same
19+
importScripts('/__/firebase/' + version + '/firebase-app.js')
20+
importScripts('/__/firebase/' + version + '/firebase-messaging.js')
21+
importScripts('/__/firebase/init.js')
22+
}
23+
24+
// Retrieve an instance of Firebase Messaging so that it can handle background
25+
// messages.
26+
const messaging = firebase.messaging()
27+
28+
messaging.setBackgroundMessageHandler(function(payload) {
29+
const data = payload.data
30+
const notificationTitle = data.title
31+
const notificationOptions = {
32+
body: data.message,
33+
icon: data.iconPath,
34+
vibrate: [200, 100, 200, 100, 200, 100, 200],
35+
actions: [
36+
// See MARK 1 -> First item is always taken as click action
37+
{
38+
title: 'Visit',
39+
action: data.clickPath
40+
}
41+
]
42+
}
43+
return self.registration.showNotification(
44+
notificationTitle,
45+
notificationOptions
46+
)
47+
})
48+
49+
self.addEventListener('notificationclick', function(e) {
50+
const notification = e.notification
51+
// MARK 1 -> always takes first item
52+
const clickAction = notification.actions[0].action
53+
const action = e.action
54+
if (action === 'close') {
55+
notification.close()
56+
} else {
57+
clients.openWindow(clickAction)
58+
notification.close()
59+
}
60+
})

0 commit comments

Comments
 (0)