2018-11-25 16:37:34 +01:00
|
|
|
import 'firebase/messaging'
|
|
|
|
import firebase from 'firebase/app'
|
|
|
|
import localforage from 'localforage'
|
|
|
|
|
|
|
|
const firebaseCloudMessaging = {
|
|
|
|
tokenInlocalforage: async () => {
|
|
|
|
return localforage.getItem('fcm_token')
|
|
|
|
},
|
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
init: async function () {
|
2018-11-25 16:37:34 +01:00
|
|
|
firebase.initializeApp({
|
2020-03-20 10:15:45 +01:00
|
|
|
apiKey: 'YOUR-API-KEY',
|
|
|
|
projectId: 'YOUR-PROJECT-ID',
|
|
|
|
messagingSenderId: 'YOUR-SENDER-ID',
|
|
|
|
appId: 'YOUR-APP-ID',
|
2018-11-25 16:37:34 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
try {
|
|
|
|
if ((await this.tokenInlocalforage()) !== null) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const messaging = firebase.messaging()
|
2020-11-12 00:41:37 +01:00
|
|
|
await Notification.requestPermission()
|
2018-11-25 16:37:34 +01:00
|
|
|
const token = await messaging.getToken()
|
|
|
|
|
|
|
|
localforage.setItem('fcm_token', token)
|
|
|
|
console.log('fcm_token', token)
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
}
|
2019-11-11 04:24:53 +01:00
|
|
|
},
|
2018-11-25 16:37:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export { firebaseCloudMessaging }
|