[英]Using curl to send data push notification with firebase cloud messaging
[英]Firebase Doesn't send push notification
我曾经发送推送通知,其中只有短信。 当时的payload结构不同,利用了function“sendToDevices(token,payload)”。 但是现在我想向其中包含图像的多个设备发送推送通知,因此我正在使用“sendMulticast(payload)”function。它可以工作到 Firebase,这意味着我可以看到控制台消息“推送通知发送”在 Firebase 控制台中,但未发送任何通知。 此外,我在控制台中测试了有效负载,一切似乎都正常。
旧代码及其有效载荷
exports.sendPushNotificationToAllDevices = functions.https.onRequest((request, response) => {
const domain = request.body.Domain;
if (typeof domain == "undefined" || domain == null || domain == "") {
response.status(400).send("Domain is empty.");
return;
}
const url = domain + '/Users';
admin.database().ref(url).once('value')
.then(snapshot => {
let tokens = [];
let counter = 0;
const count = snapshot.numChildren();
snapshot.forEach(function (data) {
counter++;
const token = data.val().FirebaseToken;
if (typeof token !== "undefined" && token !== null && token != "") {
tokens.push(token);
}
});
let uniqeTokens = [...new Set(tokens)];
if (uniqeTokens.length > 0) {
const payload = getNotificationPayload(request);
console.log(uniqeTokens.length);
const tokensChunk = chunk(uniqeTokens,999);
tokensChunk.forEach(function(tokens){
sendPushNotificationTo(tokens, payload); // sendToDevice() function is used.
});
}
});
response.status(200).send("sending");
});
旧的有效载荷结构
return {
notification: {
title: notificationTitle,
body: notificationBody,
clickAction: clickAction,
sound: 'default'
},
data: notificationData
};
新代码结构
exports.sendPushNotificationToAllDevices = functions.https.onRequest((request, response) => {
const domain = request.body.Domain;
if (typeof domain == "undefined" || domain == null || domain == "") {
response.status(400).send("Domain is empty");
return;
}
const url = domain + '/Users';
admin.database().ref(url).once('value')
.then(snapshot => {
let tokens = [];
let counter = [];
const count = snapshot.numChildren();
snapshot.forEach(function (data){
counter++;
const token = data.val().FirebaseToken;
if (typeof token !== "undefined" && token !== null && token != "") {
tokens.push(token);
}
});
let uniqueTokens = [...new Set(tokens)];
if (uniqueTokens.length > 0) {
var payload = getNotificationPayload(request);
const tokensChunk = chunk(uniqueTokens, 999);
tokensChunk.forEach(function(tokens){
payload['tokens'] = tokens;
sendPushNotificationTo(payload); // sendMulticast() function is used.
});
}
});
response.status(200).send("Sending");
});
新的有效载荷结构
let message = {
notification: {
title: notificationTitle,
body: notificationBody,
clickAction: clickAction,
sound: 'default',
},
data: notificationData,
android: {
notification: {
sound: 'default'
}
},
apns: {
payload: {
aps: {
'mutable-content': 1,
sound:sound,
}
}
}
};
if (typeof clickAction === 'string' || clickAction !== "") {
message["android"]["notification"]["click_action"] = clickAction;
}
if (typeof imageUrl === 'string' || imageUrl !== "") {
message["android"]["notification"]["imageUrl"] = imageUrl;
message["apns"]["fcm_options"]["image"] = imageUrl;
}
return message;
新的 function“sendMulticast()”的有效负载结构不正确。
所需的有效负载结构
let message = {
notification: {
title: notificationTitle,
body: notificationBody,
// clickAction: clickAction,
// sound: 'default',
},
data: notificationData,
android: {
notification: {
sound: 'default'
}
},
apns: {
payload: {
aps: {
"mutable-content": 1,
sound:'default',
}
}
}
};
if (typeof clickAction === 'string' || clickAction !== "") {
message["android"]["notification"]["click_action"] = clickAction;
}
if (typeof imageURL !== "undefined" && imageURL !== null && imageUrl !== "") {
message["android"]["notification"]["imageUrl"] = imageUrl;
}
return message;
sendMulticast() 不支持 ClickAction 和 Sound,send() 蚀刻类型为 function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.