3

This is what my log looks like when my push notification gets called on火力地堡云消息推送通知送交设备

我目前正在创建推送通知设置为用户用户设置的iPhone。我目前正在使用Firebase,所以很自然地转向使用Firebase Cloud Messaging来完成此项任务。这是我部署到我的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; 
} 



}); 
+0

你上传了firebase云消息部分的p12吗? –

+0

嘿谢谢你的回复!我不太确定p12是什么? @JigneshMayani – oneQuestionAsker

+0

您是否在回应中发生错误? –

回答

-1

发送此jsone在registration_ids字段中输入您之后的参数,你必须张贴您的所有设备令牌的数组,你要发送推送通知 这是POST请求的方法体

{ "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

和发送key="Your Authorization key"在要求外商投资企业的HTTPHeader云通讯的基本设置形式LD

采取参考这里

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

+0

在OP的情况下,他明确使用Firebase Admin模块发出请求(通过admin.messaging())。我不会按照你在这里的建议发出HTTP请求。您引用的链接仅引用Swift和Objective C,OP的问题与JS库有关。仅仅因为这些原因,我就低估了你的答案。 – AlxVallejo

0

我注意到,您两次暴露相同的功能。这也是一个问题。此外,我建议你promisify 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"); 
     });