无法在Android的Firebase实时数据库内的写入事件上使用FCM时向用户推送通知

时间:2018-08-24 15:17:02

标签: javascript android firebase firebase-realtime-database

大家好,我是的新朋友

我的Android应用程序遇到问题 问题是我正在尝试使云功能在我的实时firebase数据库的节点发生更改时被调用,并通过通知将该更改通知所有用户。 >

这些是我收到的错误的Firebase日志。

enter image description here

// imports firebase-functions module
const functions = require('firebase-functions');
// imports firebase-admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

/* Listens for new messages added to /messages/:pushId and sends a notification to subscribed users */

exports.pushNotification = functions.database.ref('/messages/{pushId}').onWrite(( change,context) => {
console.log('Push notification event triggered');

/* Grab the current value of what was written to the Realtime Database */

    const valueObject = change.after.val();
    
/* Create a notification and data payload. They contain the notification information, and message to be sent respectively */ 

    const payload = {
        notification: {
            "title": 'App Name',
            "body": "New message",
            "sound": "default"
        },
        data: {
            title: valueObject.title,
            messagge: valueObject.messagge
        }
    };

    /* Create an options object that contains the time to live for the notification and the priority. */
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24 //24 hours
    };
return admin.messaging().sendToTopic("notifications", payload, options).then(function(response){
         console.log('Notification sent successfully:',response);
    }) 
    .catch(function(error){
         console.log('Notification sent failed:',error);
    });
    }
);

0 个答案:

没有答案