iOS推送通知未收到(Firebase云功能)

时间:2018-01-29 19:47:36

标签: javascript firebase firebase-cloud-messaging google-cloud-functions

大家好!我在iOS上收到通知时遇到问题。 任务的本质是:当向数据库添加新记录时,您需要向符合条件的所有用户发送通知。在Android上一切正常,但在iOS上 - 通知无法接收。

它的云功能代码:

exports.createJob  = functions.firestore
  .document('Jobs/{jobId}')
  .onCreate(event => {
    var jobObject = event.data.data();
    var jobId=jobObject["jobId"]; 
    var jobCategoryId=jobObject["categoryId"];

    var db = admin.firestore(); 

    var nodeToSearch="categoriesMap.".concat(jobCategoryId);
    console.log("new job was added");
    console.log("createJob nodeToSearch".concat(nodeToSearch));

    db.collection('Companies')
        .where(nodeToSearch, '==', true)

        .get()
        .then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                console.log("Company, that suit: ",doc.id, " => ", doc.data().name);
                var registrationToken=doc.data().uidPhone;

                var payload = {
                      data: {
                        notificationType: "NewJobNear",
                        message: "New Job For You",
                        jobId : jobId
                      }
                    };
                    if(registrationToken){
                        console.log("Try to Send to: ",doc.id, " => ", doc.data().name);
                        admin.messaging().sendToDevice(registrationToken, payload)
                              .then(function(response) {
                                // See the MessagingDevicesResponse reference documentation for
                                // the contents of response.
                                console.log("createJob Successfully sent message:", response); 
                                console.log("Success sending ",doc.id, " => ", doc.data().name);
                              })
                              .catch(function(error) {
                                console.log("createJob Error sending message:", error);
                                console.log("Error sending ",doc.id, " => ", doc.data().name);
                            });     
                    }       
            });
        })
        .catch(function(error) {
            console.log("Error getting documents: ", error);
        }); 
    return event.data.ref.set({jobId:event.params.jobId},{merge:true}); 
});

已添加证书。当我通过来自控制台的令牌推送通知发送到特定设备时,通知会收到。

我会很乐意帮助你。

UPD: 在功能的日志中,会显示一条消息,表明通知已成功发送给用户。这一行:

console.log("createJob Successfully sent message:", response);
console.log("Success sending ",doc.id, " => ", doc.data().name);

1 个答案:

答案 0 :(得分:0)

我与您遇到相同的情况,并使用了通知消息类型的有效负载而不是数据消息类型。该问题已解决。

Firebase Cloud Messaging支持两种消息类型:通知消息和数据消息。请参阅official document

{
  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    }
  }
}