【发布时间】:2018-01-29 19:47:36
【问题描述】:
大家!我在 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});
});
已添加证书。当我通过控制台的令牌推送通知发送到特定设备时,通知会收到。
你的帮助我会很高兴的。
更新: 在函数的日志中,会显示一条消息,通知已成功发送给用户。这行:
console.log("createJob Successfully sent message:", response);
console.log("Success sending ",doc.id, " => ", doc.data().name);
【问题讨论】:
-
看起来您并没有等待您正在执行的所有异步工作的 all 承诺。你只是退回其中一个。将它们全部收集到一个数组中并返回 Promise.all() 的值,以确保您等待所有这些。
-
@DougStevenson 你能详细回答一下吗?
-
不确定,但看起来您有两种通知:数据类型和通知类型。我最近注意到在 iOS 中接收通知更容易。尝试在有效负载中使用通知而不是数据。
-
你解决了吗?
标签: javascript firebase firebase-cloud-messaging google-cloud-functions