2

Im experiencing a bad behavior using cordova on android, while developing with ionic framework, together with ngCordova plugins. With the PushPlugin plugin, Im able to receive notifications using GCM. When the app is in background and a notification is received, I can dismiss it without entering the app, but then, when I launch the app Im not aware of the new data received (& dismissed) is there a way to still have the data? I need it since its not only a notification, but also data I need to show on the UI later on.

buddy123
  • 5,679
  • 10
  • 47
  • 73
  • in which platform do you have this problem? on iOS it's how it works, but on android I think you should be able to get the push even if it's dismissed. – jcesarmobile Mar 09 '15 at 08:21
  • Its good to know that is how it works in IOS, I didnt know. but in my case Im trying it in Android. I started working on an angle to dig into the java code and use localStorage as an interface to my app, but maybe since its the same with ios I should do it differently. – buddy123 Mar 09 '15 at 08:45
  • on android the app get the push messagen and display it, so you might take a look into the plugin code and make some changes to persist the push data when it receives it. But that will involve storing them in a native way (like an sqlite database) and then another plugin to get the notifications, it won't be easy – jcesarmobile Mar 09 '15 at 08:50

2 Answers2

1

This isn't possible unfortunately. If the user clicks the notification you can execute some code. If the user dismisses it, your app will never know.

How I would build that functionality is have a database that holds all the information that a user needs to see. When the user sees the information (by opening a notification or otherwise), make a call to the sever and mark that content as read.

That way you could call your server when the app launches to get a list of content to show the user. If a notification is clicked you could take them directly to that data and then hit the server and mark it as 'viewed', or whatever.

Hope that helps, Good luck!

  • Thats one option I thought of, but makes it feel with "delay", as the user knows the data was received, and now when he opens the app he needs to wait a few seconds till he gets the data again. Is it, perhaps, possible to access localStorage from android? I know android coding (Im actually re-writing my app in ionic in order to have both platforms at once - android & ios), Perhaps I can write some code inside the gcmIntentService to access my webView's localStorage? – buddy123 Mar 09 '15 at 05:11
  • Yes, you can access local storage with by calling something like:window.localStorag – justinschuldt Mar 09 '15 at 13:54
  • I ran out of time editing my last comment, so here it is: Yes, you can access local storage with by calling something like: var newData = window.localStorage.yourVariable; The 'Angular' way would be to use their wrapper, $window, because it has promises built in. You need to include $window as a dependency in your controller or service. it might look something like this: .factory('dataService', function ($q, $window) { return newData = $window.localStorage.yourVariable; }); – justinschuldt Mar 09 '15 at 14:01
  • You're talking about how to approach localStorage from angular. That I know, and it is how I store my data. What I was wondering if its possible to access the same localStorage from within android gcm service? – buddy123 Mar 09 '15 at 17:56
  • Oh, I see what you're saying. You should be able to access local storage. Whatever you store in the window.localStorage object is actually stored in a SQLLite database file at: data/data//app_database/file_0.localstorage. I would assume you can access that from within the gcm service. Hope that helps! Here is where i found that path: http://wpcertification.blogspot.com/2012/07/reading-data-stored-in-localstroage-by.html – justinschuldt Mar 12 '15 at 14:04
0

You could achieve this behavior by modifying the plugin to cache the content from the server if application is in background. That way you will get the message even if the notification was dismissed. Read my answer on modifying the plugin.

Modify this function by adding PushPlugin.sendExtras(extras) that will send the data regardless of whether the app is in foreground or background.

protected void onMessage(Context context, Intent intent) {
    Log.d(TAG, "onMessage - context: " + context);
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
        if (PushPlugin.isInForeground()) {
            extras.putBoolean("foreground", true);
        }
        else {
            extras.putBoolean("foreground", false);
            if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                createNotification(context, extras);
            }
        }
        // call sendExtras always
        PushPlugin.sendExtras(extras);
    }
}
Community
  • 1
  • 1
Ajoy
  • 1,838
  • 3
  • 30
  • 57