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

3

当我的推送通知被调用时,我的日志看起来像这样

我目前正在创建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;
}



});

嘿,谢谢回复!我不太确定p12是什么?@JigneshMayani - oneQuestionAsker
你在响应中收到了错误吗? - AL.
首先,APNS有两个证书,一个用于开发(如果您在设备上直接编译ipa),另一个用于分发(如果您使用testflight或appStore)。在Firebase中,您必须指定开发或生产环境,并选择相应的证书。由开发人员签名的应用程序会获得证书开发的令牌(不适用于分发),反之亦然。 - Jose Pose S
我目前只有开发APNs证书与我的Firebase相关联,我没有附加任何生产APNs证书。@JosePoseS - oneQuestionAsker
我也遇到了同样的问题。Android设备可以收到推送通知,但iOS设备却不能。有解决方法吗? - Hashan Seneviratne
显示剩余2条评论
2个回答

0

我注意到你重复暴露了同一个函数,这也是一个问题。此外,我建议你将admin.messaging promisify化,以便你可以处理和检查错误。

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

在注册ID字段中发送此JSON作为您的POST参数,您必须发布您想要发送推送通知的所有设备令牌的数组。这是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"
   }
}

这是帖子的URL

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

并在请求的 HttpHeader 字段中发送 key="你的授权密钥"

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

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


在OP的情况下,他显然是使用Firebase Admin模块通过admin.messaging()进行请求。我不会像你在这里建议的那样发出HTTP请求。你提到的链接只涉及Swift和Objective C,而OP的问题是关于JS库的。仅仅因为这些原因,我会给你的答案点个踩。 - AlxVallejo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接