网络推送通知在我的Vue应用程序上不起作用

时间:2019-01-08 08:05:02

标签: javascript vue.js push-notification google-cloud-firestore firebase-cloud-messaging

我正在使用Firestore和vuejs处理Web推送通知。我要执行的操作是在表的值更改时向我的应用发送通知。我已经部署了云功能并设置了服务工作者。 这是我的代码

> const functions = require('firebase-functions');
> 
> const admin = require('firebase-admin'); admin.initializeApp();
> 
> exports.notifyNewMessage = functions.firestore  
> .document('plc_value/{id_value}')   .onUpdate((change, context) => {
>     const newValue = change.after.data();
>     const previousValue = change.before.data();
>     const name = newValue.name;
> 
>         return admin.firestore().doc('users/' + recipientId).get().then(userDoc => {
>             const registrationTokens = userDoc.get('registrationTokens')
> 
>             const notificationBody = (message['type'] === "TEXT") ? message['text'] : "You received a new image message."
>             const payload = {
>                 notification: {
>                     title: senderName + " sent you a message.",
>                     body: notificationBody,
>                     icon: 'https://content.cdn.intranetdashboard.com/images/icon-push-notifications.png'
>                 }
>             }
> 
>             return admin.messaging().sendToDevice(registrationTokens, payload).then( response => {
>                 const stillRegisteredTokens = registrationTokens
> 
>                 response.results.forEach((result, index) => {
>                     const error = result.error
>                     if (error) {
>                         const failedRegistrationToken = registrationTokens[index]
>                         console.error('blah', failedRegistrationToken, error)
>                         if (error.code === 'messaging/invalid-registration-token'
>                             || error.code === 'messaging/registration-token-not-registered') {
>                                 const failedIndex = stillRegisteredTokens.indexOf(failedRegistrationToken)
>                                 if (failedIndex > -1) {
>                                     stillRegisteredTokens.splice(failedIndex, 1)
>                                 }
>                             }
>                     }
>                 })
> 
>                 return admin.firestore().doc("users/" + recipientId).update({
>                     registrationTokens: stillRegisteredTokens
>                 })
>             })
>         })
>     })

0 个答案:

没有答案