【问题标题】:Firebase Cloud Messaging push notification not being sent to deviceFirebase 云消息推送通知未发送到设备
【发布时间】:2017-10-04 15:29:10
【问题描述】:

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



});

【问题讨论】:

  • 你上传 p12 到 firebase 云消息部分了吗?
  • 您好,感谢您的回复!我不太确定p12是什么? @JigneshMayani
  • 您是否收到响应错误?
  • 第一个 APNS 有 2 个证书 1 用于开发(如果您通过 ipa,则直接在设备上编译)或分发(如果您使用 testflight 或 appStore)在 fire base 您必须指定开发或生产并选择证书.由开发签名的应用获得证书开发的令牌(不适用于分发),反之亦然。
  • 我用一张关于@AL 日志的图片更新了问题。

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


【解决方案1】:

我注意到您公开了两次相同的功能。这也是一个问题。另外我建议您承诺 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");
        });

【讨论】:

    【解决方案2】:

    在 registration_ids 字段中的 post 参数上发送此 jsone,您必须发布要发送推送通知的所有设备令牌的数组 这是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

    并在请求 HttpHeader 字段中发送key="Your Authorization key"

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

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

    【讨论】:

    • 在 OP 的案例中,他显然是在使用 Firebase 管理模块来发出请求(通过 admin.messaging())。我不会像你在这里建议的那样发出 HTTP 请求。您引用的链接仅引用了 Swift 和 Objective C 和 OP 的问题是关于 JS 库的。仅出于这些原因,我不赞成您的回答。
    猜你喜欢
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2017-08-28
    • 2015-07-17
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多