gpt4 book ai didi

ios - Firebase Cloud Messaging 推送通知未发送到设备

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:24 25 4
gpt4 key购买 nike

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;
}



});

最佳答案

我注意到您公开了同一个函数两次。这也是一个问题。另外我建议你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");
});

关于ios - Firebase Cloud Messaging 推送通知未发送到设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com