有时某些用户会收到通知,但其他用户不会
Sometimes notifications are received for some users but for others dont
我正在使用 Firebase Functions 让我的商店客户在任何购买其商店的用户下新订单时收到通知。
我设法做到了,而且效果很好,商店收到了通知
exports.onNewOrderCreated = functions.firestore
.document('orders/{orderId}').onCreate((snap, context) => {
var db = admin.firestore();
try{
const docData = snap.data()
var shopId = docData.shopId;
var userName = docData.userName;
var total = docData.total;
}catch(error){
return handleErrorToUser(error);
}
var shopUserRef = db.collection('user').where('shop', '==', shopId);
return shopUserRef.get().then(userDoc => {
return userDoc.forEach(doc => {
const deviceToken = doc.data().deviceToken
const payload = {
notification: {
title: "You have a new order !! ️ ",
body: String(userName+" placed a new order for $"+total),
}
}
console.log("userId:"+doc.data().displayName+" deviceToken"+deviceToken)
return admin.messaging().sendToDevice(deviceToken,payload)
});
});
});
所以这个函数的作用是,当订单集合下有新订单时,它会检查该订单的shopId,然后它会迭代到users集合中,通过shopId找到店主,找到店主后,我会生成通知并将其发送给店主。
现在我想当创建了这么多订单时,这个功能不能正常工作,我不知道为什么
今天有一位商店顾客说新订单的通知从未到达。
除了我可以添加到此功能以使其正常工作之外还有什么吗?
尝试添加选项,因为 IOS 手机:
let options = {
priority: "high",
timeToLive: 24*60*60 // Choose any convenient number of seconds.
}
return admin.messaging().sendToDevice(deviceToken, payload, options)
根据Setting the priority of a message:
您可以设置优先级如下
android:{
"priority":"normal"
},
和 TTL:
"android":{
"ttl":"4500s"
},
应用于您的代码应该是:
{
notification: {
title: "You have a new order !! ️ ",
body: String(userName+" placed a new order for $"+total),
},
android:{
"priority":"high",
"ttl":"4500s"
}
}
}
我想知道这个案例是否也与折叠的消息有关。我发现这条 关于 collapsed
、non-collapsed
条消息,您可能会感兴趣。下面总结一下答案。
根据the message concepts and options documentation:
Use case scenario:
- Non-collapsible: Every message is important to the client app and needs to be delivered.
- Collapsible: When there is a newer message that renders an older, related message irrelevant to the client app, FCM replaces the older message. For example: messages used to initiate a data sync from the server, or outdated notification messages.
还有:
messages are non-collapsible by default except for notification messages, which are always collapsible
但后来在同一页上,它继续说:
except for notification messages, all messages are non-collapsible by default
这有点模棱两可。但是,在 payload section 中,它指出:
[notification messages] may have optional data payload. Always collapsible
因此,似乎无法发出通知消息 non-collapsible。我建议发送 data-only 负载。
Android quickstart sample 上有一个例子:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// ...
}
// ...
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
那里的最后一条评论指向示例 sendNotification()
方法。
我正在使用 Firebase Functions 让我的商店客户在任何购买其商店的用户下新订单时收到通知。
我设法做到了,而且效果很好,商店收到了通知
exports.onNewOrderCreated = functions.firestore
.document('orders/{orderId}').onCreate((snap, context) => {
var db = admin.firestore();
try{
const docData = snap.data()
var shopId = docData.shopId;
var userName = docData.userName;
var total = docData.total;
}catch(error){
return handleErrorToUser(error);
}
var shopUserRef = db.collection('user').where('shop', '==', shopId);
return shopUserRef.get().then(userDoc => {
return userDoc.forEach(doc => {
const deviceToken = doc.data().deviceToken
const payload = {
notification: {
title: "You have a new order !! ️ ",
body: String(userName+" placed a new order for $"+total),
}
}
console.log("userId:"+doc.data().displayName+" deviceToken"+deviceToken)
return admin.messaging().sendToDevice(deviceToken,payload)
});
});
});
所以这个函数的作用是,当订单集合下有新订单时,它会检查该订单的shopId,然后它会迭代到users集合中,通过shopId找到店主,找到店主后,我会生成通知并将其发送给店主。
现在我想当创建了这么多订单时,这个功能不能正常工作,我不知道为什么
今天有一位商店顾客说新订单的通知从未到达。
除了我可以添加到此功能以使其正常工作之外还有什么吗?
尝试添加选项,因为 IOS 手机:
let options = {
priority: "high",
timeToLive: 24*60*60 // Choose any convenient number of seconds.
}
return admin.messaging().sendToDevice(deviceToken, payload, options)
根据Setting the priority of a message:
您可以设置优先级如下
android:{
"priority":"normal"
},
和 TTL:
"android":{
"ttl":"4500s"
},
应用于您的代码应该是:
{
notification: {
title: "You have a new order !! ️ ",
body: String(userName+" placed a new order for $"+total),
},
android:{
"priority":"high",
"ttl":"4500s"
}
}
}
我想知道这个案例是否也与折叠的消息有关。我发现这条 collapsed
、non-collapsed
条消息,您可能会感兴趣。下面总结一下答案。
根据the message concepts and options documentation:
Use case scenario:
- Non-collapsible: Every message is important to the client app and needs to be delivered.
- Collapsible: When there is a newer message that renders an older, related message irrelevant to the client app, FCM replaces the older message. For example: messages used to initiate a data sync from the server, or outdated notification messages.
还有:
messages are non-collapsible by default except for notification messages, which are always collapsible
但后来在同一页上,它继续说:
except for notification messages, all messages are non-collapsible by default
这有点模棱两可。但是,在 payload section 中,它指出:
[notification messages] may have optional data payload. Always collapsible
因此,似乎无法发出通知消息 non-collapsible。我建议发送 data-only 负载。
Android quickstart sample 上有一个例子:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// ...
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
// ...
}
// ...
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
那里的最后一条评论指向示例 sendNotification()
方法。