Firebase 不发送推送通知
Firebase Doesn't send push notification
我以前发送推送通知时只包含短信。当时的payload结构不同,利用了函数“sendToDevices(token,payload)”。但是现在我想向其中包含图像的多个设备发送推送通知,因此我正在使用“sendMulticast(payload)”函数。它适用于 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;
新函数“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()、send() 蚀刻类函数不支持 ClickAction 和 Sound
我以前发送推送通知时只包含短信。当时的payload结构不同,利用了函数“sendToDevices(token,payload)”。但是现在我想向其中包含图像的多个设备发送推送通知,因此我正在使用“sendMulticast(payload)”函数。它适用于 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;
新函数“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()、send() 蚀刻类函数不支持 ClickAction 和 Sound