Firebase currently rolled out Firebase Functions to add server side code.
I am currently using this approach for notifications on receiving a message from a sender.
Everything seems fine but when i kill the app I dont receive notifications.
I saw some answers regarding it that i should use just data message and receive in onMessageReceived()
but its not working for killed app. What should i do?
NodeJS Index.js
exports.sendNewMessageNotification = functions.database.ref('/rootssahaj/authGplus/users/{userTorS}/{userTeacherUID}/messages/{chatWithOthUser}/{messageUID}').onWrite(event => {
console.log('called1 ');
const TeacherUid = event.params.userTeacherUID;
const whoTorS=event.params.userTorS;
var whoOppTorS=null;
if (whoTorS=="teachers") {
whoOppTorS="students";
}else{
whoOppTorS="teachers";
}
var StudentUid = event.params.chatWithOthUser;
StudentUid=StudentUid.substring(8);
console.log('called2 ')
if (!event.data.val()) {
return console.log('No Change ');
}
console.log('Event data: ',StudentUid, event.data.val());
if (StudentUid!=event.data.val().sender) {
return console.log('Different sender ',event.data.val().sender);
}
// Get the list of device notification tokens.
const getDeviceTokensPromise = admin.database().ref(`/rootssahaj/authGplus/users/${whoTorS}/${TeacherUid}/profile/fcmtoken`).once('value');
// Get the follower profile.
const getFollowerProfilePromise = admin.database().ref(`/rootssahaj/authGplus/users/${whoOppTorS}/${StudentUid}/profile`).once('value');
return Promise.all([getDeviceTokensPromise, getFollowerProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const follower = results[1];
console.log('Token: ', tokensSnapshot.val(),' ',follower.val());
// Check if there are any device tokens.
if (tokensSnapshot.val()==null) {
return console.log('There are no notification tokens to send to.');
}
console.log('There are', tokensSnapshot.numChildren(), tokensSnapshot,'tokens to send notifications to.');
console.log('Fetched follower profile', follower.val().userNAME);
// Notification details.
const payload = {
data: {
body: `new message: ${event.data.val().text}`,
title: `${follower.val().userNAME}`,
}
};
var options = {
priority: "high"
};
// Listing all tokens.
//const tokens = Object.keys(tokensSnapshot.val());
// console.log('tokens', tokens);
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokensSnapshot.val(), payload,options).then(response => {
// For each message check if there was an error.
const tokensToRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
//tokensToRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
return Promise.all(tokensToRemove);
});
});
});
via Sahaj Rana
No comments:
Post a Comment