/
firebaseMessaging.js
104 lines (95 loc) · 2.88 KB
/
firebaseMessaging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { get } from 'lodash'
import firebase from 'firebase/app'
import messageActions from 'modules/notification'
import { publicVapidKey } from '../config'
import 'firebase/messaging'
/**
* Write FCM messagingToken to user profile
* @param {String} messagingToken - Token to be written to user profile
*/
function updateUserProfileWithToken(messagingToken) {
const currentUserUid =
firebase.auth().currentUser && firebase.auth().currentUser.uid
if (!currentUserUid) {
return Promise.resolve()
}
return firebase
.firestore()
.collection('users')
.doc(currentUserUid)
.update({
messaging: {
mostRecentToken: messagingToken,
updatedAt: firebase.firestore.FieldValue.serverTimestamp()
}
})
}
/**
* Get messaging token from Firebase messaging
*/
function getMessagingToken() {
return firebase
.messaging()
.getToken()
.catch(err => {
console.error('Unable to retrieve refreshed token ', err) // eslint-disable-line no-console
return Promise.reject(err)
})
}
/**
* Get Cloud Messaging Token and write it to the currently logged
* in user's profile
*/
function getTokenAndWriteToProfile() {
return getMessagingToken().then(updateUserProfileWithToken)
}
/**
* Request permission from the user to display display
* browser notifications
*/
export function requestPermission() {
return firebase
.messaging()
.requestPermission()
.then(getTokenAndWriteToProfile)
.catch(err => {
console.error('Unable to get permission to notify: ', err) // eslint-disable-line no-console
return Promise.reject(err)
})
}
/**
* Setup Firebase Cloud Messaging. This requests permission from the
* user to show browser notifications. If the user approves or if they have
* approved in the passed, then a Cloud Messaging Token is written to the
* user's profile.
* @param {Function} dispatch - redux action dispatching function
*/
export function initializeMessaging(dispatch) {
const messaging = firebase.messaging()
if (!publicVapidKey) {
/* eslint-disable no-console */
console.warn(
'Skipping messaging initialization, publicVapidKey not set in src/config.js'
)
/* eslint-enable no-console */
return
}
messaging.usePublicVapidKey(publicVapidKey)
// Handle Instance ID token updates
messaging.onTokenRefresh(() => {
getTokenAndWriteToProfile()
})
// Handle incoming messages. Called when:
// - a message is received while the app has focus
// - the user clicks on an app notification created by a service worker
// `messaging.setBackgroundMessageHandler` handler.
messaging.onMessage(payload => {
const DEFAULT_MESSAGE = 'Message!'
// Dispatch showSuccess action
messageActions.showSuccess(
get(payload, 'notification.body', DEFAULT_MESSAGE)
)(dispatch)
})
// Request permission to setup browser notifications
requestPermission()
}