push notification does not receive(sound does not play) when phone locks for more then 10 minutes.
But when I unlocks my phone then instantly I receive notification irrespective of whether app is running or not. Notifications are not coming only when phone is in lock state.
var androidConfig = {
"senderID": "replace_with_sender_id",
};
document.addEventListener("deviceready", function(){
$cordovaPush.register(config).then(function(result) {
// Success
}, function(err) {
// Error
})
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
}
break;
case 'message':
// this is the actual push notification. its format depends on the data model from the push server
alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
break;
case 'error':
alert('GCM error = ' + notification.msg);
break;
default:
alert('An unknown GCM event has occurred');
break;
}
});
// WARNING: dangerous to unregister (results in loss of tokenID)
$cordovaPush.unregister(options).then(function(result) {
// Success!
}, function(err) {
// Error
})
}, false);
});
Actually problem is that I am not getting notification after locking screen for some time. other wise it is working perfectly.
I was able to get everything working with slight modification of this tutorial. I have the following factories:
angular.module('pushnotification', [])
.factory(("ionPlatform"), function( $q ){
var ready = $q.defer();
ionic.Platform.ready(function( device ){
ready.resolve( device );
});
return {
ready: ready.promise
}
})
.factory(("PushService"), function($rootScope, $cordovaPush, $cordovaDialogs, UserData, $firebase){
var register = function () {
var config = null;
if (ionic.Platform.isAndroid()) {
config = {
"senderID": **YOUR_GCM_ID**
};
}
else if (ionic.Platform.isIOS()) {
config = {
"badge": "true",
"sound": "true",
"alert": "true"
}
}
/* Function to register; only iOS registration completed here; Android is below */
$cordovaPush.register(config).then(function (result) {
if (ionic.Platform.isIOS()) {
/* Code to save result to database or localStorage. I used Firebase */
}
}, function (err) {
console.log("Register error " + err)
});
}
// Notification Received
$rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
if (ionic.Platform.isAndroid()) {
handleAndroid(notification);
}
else if (ionic.Platform.isIOS()) {
handleIOS(notification);
}
});
// Android Notification Received Handler
function handleAndroid(notification) {
if (notification.event == "registered") {
/* Save registration ID (notification.regid) to Firebase */
}
else if (notification.event == "message") {
$cordovaDialogs.alert(notification.message, 'Update');
}
}
// IOS Notification Received Handler
function handleIOS(notification) {
// The app was already open but we'll still show the alert and sound the tone received this way.
if (notification.foreground == "1") {
if (notification.body && notification.messageFrom) {
$cordovaDialogs.alert(notification.body, notification.messageFrom);
}
else $cordovaDialogs.alert(notification.alert, "Update");
if (notification.badge) {
$cordovaPush.setBadgeNumber(notification.badge).then(function (result) {
console.log("Set badge success " + result)
}, function (err) {
console.log("Set badge error " + err)
});
}
if (notification.sound) {
var media = new Media(notification.sound);
media.play();
}
}
// Otherwise it was received in the background and reopened from the push notification.
else {
if (notification.body && notification.messageFrom) {
$cordovaDialogs.alert(notification.body, notification.messageFrom);
}
else $cordovaDialogs.alert(notification.alert);
}
}
return{
register: function() { return register() }
}
});
Which I called with:
/* Set up Push Notifications */
ionPlatform.ready.then(function (device) {
PushService.register();
});