Reputation: 300
I am currently working on an mobile push notification (android) using MeteorJS. I am using the package:
https://github.com/katzer/cordova-plugin-local-notifications
It works fine when the app is open but when closed nothing happens. Is there a way using this package to make the push notification works even when the app is closed or minimized? Is there an alternative package for meteor that can do background push notification? So far this is my code:
if(Meteor.isCordova){
Meteor.startup(function () {
cordova.plugins.notification.local.registerPermission(function (granted) {
if(confirm("Sample App would like to send you notifications: ") === true) {
alert('permission granted '+granted)
} else {
alert('permission denied')
}
});
});
Template.hello.events({
'click button': function () {
var msg = $('[name=msg]').val();
cordova.plugins.notification.local.add({ title: 'This is a sample push', message: msg });
}
});
};
A very straight forward code that works only when the app is open. I am clueless on how the background notification actually works. Thanks
Upvotes: 1
Views: 583
Reputation: 86
It's because you have not created a schedule
for the notification yet. If the app isn't running, then the code you've written to execute the plugin won't be running either unless it's scheduled to do so, therefore creating a background process once the app has been loaded and permissions granted.
Change your initialization of the notification from:
cordova.plugins.notification.local.add({
title: 'This is a sample push',
message: msg });
To:
date = foo; //change this to when you want the notification to fire
//for the first time
cordova.plugins.notification.local.**schedule**({
id: 1,
title: 'this is a sample push',
message: msg,
firstAt: date,
every: 'minute' //set this to how often you want it repeated.
)};
What ended up happening is the template initialized the notification without any further parameters on how it is supposed to run. This is all thoroughly explained in the packages documentation if you need further help.
Upvotes: 1