Reputation: 97
I used to send push notifications with only text messages in them. The payload structure was different at the time, and the function "sendToDevices(token,payload)" was utilized. But now I want to send a push notice to several devices with images in it, thus I'm using the "sendMulticast(payload)" function. It works up to Firebase, which means I can see the console message "push notification send" in the Firebase console, but no notification is sent. Also, I tested the payload in the console and everything seemed to be working.
Old Code and its payload
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");
});
Old Payload Structure
return {
notification: {
title: notificationTitle,
body: notificationBody,
clickAction: clickAction,
sound: 'default'
},
data: notificationData
};
New Code Structure
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");
});
New Payload Structure
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;
Upvotes: 0
Views: 778
Reputation: 97
Payload structure was not correct for new function "sendMulticast()" .
required payload structrue
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;
ClickAction and Sound is not supported by sendMulticast(), send() etch kind of function
Upvotes: 1