1

I have a parse-server deployed with Heroku (from my GitHub repo) and hosted by mongoLab. I am attempting to send scheduled push notifications within my app and kue seems to be the most viable option. However, as I am very unfamiliar with it, I am not sure the way to approach it. I believe I have correctly installed kue on my server (through GitHub). Now, I would like to schedule this code to be executed at a date in the future:

    Parse.Cloud.define("sendPush", function(request, response) {


      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('username', request.params.targetUsername);

      Parse.Push.send({
        where: pushQuery, // Set our Installation query                                                                                                                                                              
        data: {
          alert: 'Hello!',
          badge: 'Increment',
          sound: 'PopDing.caf'
        },   
      }, { success: function() {
  console.log("#### PUSH OK");
      }, error: function(error) {
  console.log("#### PUSH ERROR" + error.message);
      }, useMasterKey: true});

    });

If I am approaching this the correct way, then I need the code to simply schedule a job (the code above) to be executed at a specified time in the future. I do not the code to be scheduled to run regularly or at intervals, just once at the specified time in the future. An answer or any advice would be greatly appreciated, thanks!

Baylor Mitchell
  • 1,029
  • 1
  • 11
  • 19

1 Answers1

1

Here is an example how you can accomplish scheduling this task with kue only once at a specific time in the future: (after 12 hours)

var kue = require( 'kue' );

// create our job queue
var jobs = kue.createQueue();

// one minute
var minute = 60000;

var job= jobs.create( 'parseCloud', {
    alert: 'Hello!',
          badge: 'Increment',
          sound: 'PopDing.caf'
} ).delay( minute * 60 * 12)
  .priority( 'high' )
  .save();



job.on( 'complete', function () {
  console.log( 'renewal job completed' );
} );



jobs.process( 'parseCloud', function ( job, done ) {

      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('username', request.params.targetUsername);

      Parse.Push.send({
        where: pushQuery, // Set our Installation query                                                                                                                                                              
        data: {
          alert: job.data.alert,
          badge: job.data.badge,
          sound: job.data.sound
        },   
      }, { success: function() {

        console.log("#### PUSH OK");
        done();
      }, error: function(error) {
        console.log("#### PUSH ERROR" + error.message);
         done();
      }, useMasterKey: true});

} );

// start the UI
kue.app.listen( 3000 );
console.log( 'UI started on port 3000' );
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
  • Thank you very much for the response! I feel as though I am very close. However I have used this exact code and placed it inside a cloud function (`Parse.Cloud.define("sendPushToSpecificUser", function(request, response) { "code you provided" });`) which i call to from a device. I then receive an error and the notification is not sent. Any ideas as to why this is happening? – Baylor Mitchell May 09 '16 at 22:08
  • 1
    Hi Tal Avissar, 1. Where to use this source code? 2. If I want to start job when starting parse-server, what will the code looks like? 3. Do I need this line of code "due.app.listen(3000); ? – Phuc Tran Jun 02 '16 at 11:01