This is how I am making a firebase cloud function to send silent push notification:
exports.sendSilentPyngNotification = functions.database.ref('/SNotification/{userid}').onWrite(event => {
const userid = event.params.userid
console.log('Start sending SILENT Notificaitons:-');
// Get the list of device notification tokens for the respective user
const getDeviceTokensPromise = admin.database().ref(`/personal/${userid}/notificationTokens`).once('value');
// Get the user profile.
const getUserProfilePromise = admin.auth().getUser(userid);
return Promise.all([getDeviceTokensPromise, getUserProfilePromise]).then(results => {
const tokensSnapshot = results[0];
const user = results[1];
// Check if there are any device tokens.
if (!tokensSnapshot.hasChildren()) {
return console.log('There are no notification tokens to send to.');
}
// Notification details.
const payload = {
notification: {
content_available : 'true'
},
data : {
senderid: 'Test',
notificationtype: String(event.data.val().type)
}
};
// Listing all tokens.
const tokens = Object.keys(tokensSnapshot.val());
// Send notifications to all tokens.
return admin.messaging().sendToDevice(tokens, payload).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());
}
}
console.log('Successfull sent SILENT NOTIFICATION');
});
return Promise.all(tokensToRemove);
});
});
});
Question: I want to fire this silent notification function repeatedly using a timer . I really got no any idea how to write timer function in node.js for this kind of function.
Does anyone can quick guide me ? I am an iOS Dev with no knowledge in node.js.
Thanks.