I have set up an Ionic Framework project which receives GCM push notifications. I want to save the incoming notifications in the window.localStorage of the app.
This is what I have tried so far:
function onNotificationGCM(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
//call back to web service in Angular
var elem = angular.element(document.querySelector('[ng-app]'));
var injector = elem.injector();
var myService = injector.get('PushProcessingService');
myService.registerID(e.regid);
}
break;
case 'message':
if (e.foreground)
{
//no problem here, this works as the app is already open
window.localStorage['notifications'] = e.payload.message;
}
else
{
//saving to localStorage in here works only when the user opens the app via the received notification
if (e.coldstart)
window.localStorage['notifications'] = e.payload.message;
else
window.localStorage['notifications'] = e.payload.message;
}
break;
case 'error':
break;
default:
break;
}
}
Saving to window.localStorage works if the app is already open when the notification arrives, but when the app is closed those methods will not get called. This will only happen if the user taps on the notification, which then opens the app.
Is there a way to save notification data to the app's localStorage even if the user dismisses the incoming push notification?
A work-around would be to store all notifications on a server and make the app retrieve them on opening, but I don't want to make the app slower by making unnecessary requests.