4

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.

Martin
  • 896
  • 1
  • 7
  • 22
  • 1
    I had to write almost the exact same thing. I ended up doing a register gcm call with a callback. When the app receives a notification the callback is triggered - the callback code does a retrieve on the server. – lulu88 Jun 24 '14 at 13:10
  • This architecture seems risky to me. I wouldn't rely on GCM to deliver important information to your app. Whatever source creates the GCM notification, I would just fetch it directly from there within your app. This is also more reliable cross-platform, etc. – colllin Jul 06 '14 at 18:20
  • 1
    One of the solution could be at the basic plugin level to store all the messages at native layer itself and frontent ( javascript) can later on pull the messages from native plugin. We are facing a similar problem. I am more inclined towards implementing this solution. – pankajanand18 Aug 05 '14 at 06:07

1 Answers1

0

Assuming that GCM is reliable, you just need to cache the message. Read my answer on a related issue. So your application will still receive the notification payload even though the notification is dismissed by the user. Then you can guarantee that the data you want to save in localStorage will be delivered to you.

Note: I have not yet tested it, but will update this answer when I have.

Community
  • 1
  • 1
Ajoy
  • 1,838
  • 3
  • 30
  • 57