这是我的日志的样子,当我的推送通知被调用
我当前正在努力为用户设置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;
}
});
我注意到您正在两次公开相同的功能。这也是一个问题。我也建议您承诺管理员,以便您可以处理并检查错误。
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");
});
在registration_ids字段中的帖子参数上发送此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"
}
}
这是发布url
https://fcm.googleapis.com/fcm/send
并在请求httpheader字段中发送 key="Your Authorization key"
请参考基本设置表格,以获取云消息传递
https://firebase.google.com/docs/cloud-messaging/ios/client