Wednesday, 31 May 2017

How to send notification using node-cron in android ?

var FCM = require('fcm-push');
var serverKey = 'server-key'; // Android
var fcm = new FCM(serverKey);

var android_user = 'android device token';


var message = {
    to: android_user, // required fill with device token or topics
    // collapse_key: 'your_collapse_key',
    // data: {
    //     your_custom_data_key: 'your_custom_data_value'
    // },
    notification: {
        title: 'My APP',
        body: 'Body of your push notification'
    }
};


fcm.send(message, function(err,response){
    if(err) {
        console.log("Something has gone wrong w/o cron !");
    } else {
        console.log("Successfully sent with resposne w/o cron :",response);
    }
});

It successfully send message to android device.

Now using cron module as I want to send multiple times

var CronJob = require('cron').CronJob;

// Will run in every minute

var job1 = new cron.CronJob({
  cronTime: '* * * * *',
  onTick: function() {
    fcm.send(message, function(err,response){
        if(err) {
            console.log("Something has gone wrong !");
        } else {
            console.log("Successfully sent with resposne :",response);
        }
    });

  },
  start: false,
  timeZone: 'America/Los_Angeles'
});


job1.start(); // job 1 started

But when the same above method used with cron, then it shows that message has been sent on the console view but on android device, it will not display to the user



via VIKAS KOHLI

No comments:

Post a Comment