0

I am programming a mobile app using Cordova. I am implementing the push notifications using Push Plugin. The app is meant to run on all platforms but right now I am testing on Android and Windows.

In a particular javascript file I am saving a value call it 'category' in the localstorage:

localStorage.setItem("category", JSON.stringify(categoryarray));

Now when sending a push notification, the category is essential to decide whether to show the notification or not. If a user is subscribed to that particular category, then, the notification is to be shown, otherwise not. For this I simply create a condition and check whether the user has subscribed to the category included in the notification (but this is not really relevant to the point of the question). When the app is running this condition can be handled in javascript. When the app is not running, this is handled in java code:

else {
            extras.putBoolean("foreground", false);

            // Send a notification if there is a message
            if (extras.getString("message") != null && extras.getString("message").length() != 0) {
                createNotification(context, extras);
            }
        }

Now I want to get the value from the local storage at that instance that the notification is being pushed when the app is not running (and be able to check whether the notification should be shown or not).

I came into this link: Android Service reads localStorage?

But it seems to be meant for Android native code (reference to the webview). Apart from that I haven't really understood how it works and furthermore if it is applicable for my problem.

What do you suggest? How can I do it?

Edit: I didn't initially realise that the Push plugin java code won't be compiled with the Cordova app. So editing the code that is retrievable from the Cordova directory is in reality useless. Unless, someone can still suggest something, I know that this is an unanswerable question. Will have to re-attempt to create an API for this purpose and handle who to receive which notification at server side! (The reason why I resorted to this method was because I wasn't managing to create an API for notification purposes)

Community
  • 1
  • 1
Jurgen Cuschieri
  • 658
  • 14
  • 33

1 Answers1

0

I didn't initially realise that the Push plugin java code won't be compiled with the Cordova app. So editing the code that is retrievable from the Cordova directory is in reality useless. Unless, someone can still suggest something, I know that this is an unanswerable question.

This isn't true, Cordova plugin code is compiled when you compile your Cordova app. All Cordova plugin's provide native source code that gets compiled into the app when you run cordova build (or cordova run <platform>).

If you wanted to solve this completely on the client side (rather than managing the categories that a user is subscribed to on the backend and only sending a notification if the user is subscribed to a category), you could extend the PushPlugin to manage subscriptions to categories.

As a rough sketch:

In PushNotification.js, add a method to subscribe to a channel:

PushNotification.prototype.subscribeToChannel(successCallback, errorCallback, channel) {
    cordova.exec(successCallback, errorCallback, "PushPlugin", "subscribeToChannel", [{channel: channel}]);
}

In PushPlugin.java catch the subscribeToChannel action in the execute function:

public boolean execute(String action, JSONArray data, CallbackContext callbackContext) {
...
    if ("subscribeToChannel".equals(action)) {
        //get the attached data
        JSONObject jo = data.getJSONObject(0);
        String channel = (String) jo.get("channel");
        addChannelToSubscriptions(channel);
    }
...
}

public void addChannelToSubscriptions(String channel) {
    //store as a list in a sharedpreferences list
}

Then when a notification is received, you can check if the channel is a channel that has been subscribed to.

// Send a notification if subscribed to the channel
if (extras.getString("channel") != null && isSubscribedTo(extras.getString("channel"))) {
    createNotification(context, extras);
}

public boolean isSubscribedTo(String channel) {
    //see if the channel is in the shared preferences.
}

Personally, I think it'd be easier to manage subscriptions on the backend as to manage it in the app, you'd have to implement this logic for each platform you support. It would be easier to just add a webservice call in your Javascript. As a further alternative, if you don't want to handle the subscription logic on your backend, you could look at a service like Parse where the concept of subscribing to channels is built into the service.

JimR
  • 2,145
  • 8
  • 28
  • 37