Firebase云消息传递推送通知未发送到设备

时间:2017-05-06 05:11:05

标签: ios node.js push-notification firebase-cloud-messaging google-cloud-functions

This is what my log looks like when my push notification gets called on

我目前正致力于为用户创建针对iPhone用户设置的推送通知设置。我目前正在使用Firebase,因此我自然而然地转向使用Firebase云消息传递来完成此操作。这是我在部署到Firebase的功能中的设置。我在这里做错了会导致通知没有被发送到设备吗?我感谢任何帮助,如果有任何需要的信息,我很乐意提供。

const functions = require('firebase-functions');

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

// Listens for new messages added to messages/:pushId
exports.pushNotification =     functions.database.ref('/messages/{pushId}').onWrite( event => {

console.log('Push notification event triggered');

//  Grab the current value of what was written to the Realtime Database.
var valueObject = event.data.val();
console.log(valueObject)

if(valueObject.photoUrl != null) {
  valueObject.photoUrl= "Sent you a photo!";
}

 // Create a notification
const payload = {
    notification: {
        title:valueObject.toId, 
        body: valueObject.text || valueObject.photoUrl,
        sound: "default"
    },
};

//Create an options object that contains the time to live for the notification and the priority
const options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
};
return admin.messaging().sendToTopic("pushNotifications", payload, options);
 if(!data.changed()){

});

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



});

2 个答案:

答案 0 :(得分:0)

我注意到你暴露了两次相同的功能。这也是一个问题。另外,我建议你宣传admin.messaging,这样你就可以处理并检查错误。

let topic = "pushNotifications";
admin.messaging().sendToTopic(topic, payload, options)
        .then(function(response) {

            console.log("Successfully sent message:", response);
            console.log("Topic: " + topic);
            res.status(200).send("success");
        })
        .catch(function(error) {
            console.log("Error sending message:", error);
            res.status(500).send("failure");
        });

答案 1 :(得分:-1)

在registration_ids字段中的post参数上发送此jsone,您必须发布要发送推送通知的所有设备令牌的数组 这是请求后方法主体

{ "registration_ids" : [Send Array of Device Token], 
  "data" :
    {
    "image_url" : "send your image here"
    "message" : "Send your message here" },
 "notification" : 
   {
    "title" : "APP Name",
    "sound" : "default",
    "priority" : "high"
   }
}

这是帖子网址

https://fcm.googleapis.com/fcm/send

并在请求HttpHeader字段

中发送key="Your Authorization key"

此处参考云消息传递的基本设置表单

https://firebase.google.com/docs/cloud-messaging/ios/client