1

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
user3804063
  • 809
  • 1
  • 14
  • 32
  • 2
    Frede's approach will work for short intervals. But there is a maximum run time for each type of function, and you can't exceed that with a `setTimeout()`. Also: you'll be paying for the entire time your function is "sleeping". A more effective way would be to use an external timer service, such as cron-job.org or running on Google App Engine. See https://stackoverflow.com/questions/42790735/cloud-functions-for-firebase-trigger-on-time – Frank van Puffelen Dec 08 '17 at 14:19

1 Answers1

1

You might want to look at https://nodejs.org/en/docs/guides/timers-in-node/ The easiest way to repeat any codeblock multiple times is setInterval

// The function you want to fire multiple times
function functionToRepeat() {
    // Put your code here
}

// Calls functionToRepeat every 1500ms
var intervalObj = setInterval(functionToRepeat, 1500);

// To stop the interval 
clearInterval(intervalObj)

Hope this helps!

Frede
  • 701
  • 4
  • 14