Parse Server Scheduled Push Notifications Hack

How to setup scheduled push notifications when push-time is not supported?

I recently download parse-server for nodeJS and it does not support the push_time attribute which allows a notification to be pushed at a specific time. It only will allow instant pushes.

To work around this limitation, I used node-schedule to perform my Parse pushes at a scheduled time.

npm install node-schedule --save
npm install parse --save

Then add a call to node-schedule and Parse in your nodeJS application.

var Parse = require(‘parse/node’).Parse,
schedule = require(‘node-schedule’);

Then configure Parse to work with your Parse-server.

Parse.initialize(
<< enter application ID >>, // applicationId
<< enter master key for JS Key >>, // javaScriptKey
<< enter master key >>> // masterKey
);
Parse.serverURL = ‘<< enter url to parse-server >>';

The next step is to setup the node-schedule to use a specific date to send the push notifications. The node-schedule version 1.1.0 does not allow schedules to be sent with dates from the past. If this is a problem for your application then modify the code to identify dates in the past and schedule the notification to go out in the next minute.

var newUTDate = new Date(req.body.alertDate);   //replace date value
var currentDate = new Date(Date.now() + (1 * 60000));
if(newUTDate <= currentDate )
   newUTDate = currentDate;

The node-schedule requires date schedules to be in the format (Full Year, Month -1, Day of the Month, Hours (24 hour), Minutes, and Seconds). If the date is not in this format then the job will not be scheduled and the return value will be null.

var schRetVal= schedule.scheduleJob(new Date(
newUTDate.getFullYear(),
newUTDate.getMonth(),
newUTDate.getDate(),
newUTDate.getHours(),
newUTDate.getMinutes()),
function(){
var query = new Parse.Query(Parse.Installation)
, data = {
     "alert" :"Reminder, Michael is going to help you with basketball today",
 };
query.equalTo("userid", 23); // send push to user
query.equalTo("deviceType", "ios");

//push_time is not supported in the parse-server.
Parse.Push.send({
   where: query,
   data: data
},
{
success: function () {
   console.log("arguments", arguments);
   console.log("User reminded of work today");
},
error: function (error) {
    console.log("Error: " + error.code + " " + error.message);
},
useMasterKey: true
});
if(schRetVal)
    res.json({message: 'Success'});
else 
   res.json({message: 'Failed to Schedule Work Reminders.'});
});

An example can be found on my GitHub account. The goal of this tutorial was to provide an alternative to the push_time attribute that is not supported in parse-server. To see more of my tutorials go to my website, www.tiffanyricks.com.