I am currently working on implementing push notifications for both Android and iOS.
For this task, I have utilized a cordova push-plugin from the following link: https://github.com/phonegap-build/PushPlugin, and so far, it has been functioning excellently.
A quick note: I am working on an AngularJS project.
Within my NotificationHelper factory, I have implemented the following init method:
helper.init = function() {
// Initialize the push plugin notification
var pushNotification = window.plugins.pushNotification;
var errorHandler = function(error) {
logger.debug('errorHandler = ' + error);
};
if ($rootScope.isAndroid()) {
var senderId = CONFIG.NOTIFICATION_ANDROID_SENDER_ID;
pushNotification.register(function(result) {
logger.debug('successHandler = ' + result);
}, errorHandler, {
'senderID' : senderId,
'ecb' : 'onNotificationGCM'
});
}
};
Additionally, I have defined the following methods in mains.js :
var onNotificationGCM = function(event) {
// Callback to web service in Angular.
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var service = injector.get('NotificationHelper');
service.onNotificationGCM(event);
};
This is a workaround to call an AngularJS factory from main JavaScript.
The 'onNotificationGCM' function calls the 'NotificationHelper.onNotificationGCM' method :
helper.onNotificationGCM = function(e) {
switch (e.event) {
case 'message':
// Notification received while app was in the foreground
if (e.foreground) {
logger.debug('[notification] [message] Foreground : ' + JSON.stringify(e));
} else { // Notification tapped in the notification tray
logger.debug('[notification] [message] Background : ' + JSON.stringify(e));
if (e.coldstart) {
// App was not running and user clicked on notification
} else {
// App was running and user clicked on notification
}
}
decriptPayloadNotification(e.payload);
break;
case 'registered':
logger.debug('[notification] [registered] : ' + JSON.stringify(e));
if (e.regid.length > 0) {
registerUser(e.regid, 'gcm');
}
break;
case 'error':
logger.debug('[notification] [error] : ' + JSON.stringify(e));
break;
default:
logger.debug('[notification] [default] : Unknown, an event was received and we do not know what it is : ' + JSON.stringify(e));
break;
}
};
Initially, everything works well:
- 'Registered' event is received
- Notifications are received
- When I'm in the foreground, I receive the 'message' event :
NotificationHelper: [notification] [message] Foreground : {"event":"message","from":"847593779913","message":"Agenêts 23/03 10h\r\n","collapse_key":"do_not_collapse","foreground":true,"payload":{"lt":"school","lv":"E1154","notId":"35429","title":"Agenêts le 23/03/2015","message":"Agenêts 23/03 10h\r\n"}}
- When I'm in the background and receive a notification that I tap in the notification tray, I receive the 'message' event :
NotificationHelper: [notification] [message] Background : {"event":"message","from":"847593779913","message":"la piscine sera fermée 1 journée pour raison technique","coldstart":false,"collapse_key":"do_not_collapse","foreground":false,"payload":{"lt":"swimming pool","lv":"E114","notId":"29869","title":"23/04/2015 fermeture de la piscine","message":"la piscine sera fermée 1 journée pour raison technique"}}
However, if I force quit my app, the functionality stops.
If I restart the app, I no longer receive the 'message' events and the 'onNotificationGCM' method is not called.
I have found some articles discussing this issue, but so far, without any success:
Stackoverflow : Phonegap PushNotification to open a specific app page
Does anyone have any insights or ideas on how to address this problem?