0
votes

I'm using the Parse SDK to receive push notifications in my Android app. My backend sends a JSON formatted string with the contents of the push notification to be displayed in the notifications bar.

What I want to do is parse this JSON and depending on it's content, take the user to different parts of the app (Activities). I'm able to receive and parse the JSON fine, but how can I handle the actual action which is performed when the user taps on the notification in the notifications bar?

I know that I can configure the Parse SDK to open up different activities when I subscribe to a specific channel, like this:

    PushService.subscribe(this, "testing", ActivityA.class, R.drawable.ic_push);
    PushService.subscribe(this, "testing2", ActivityB.class, R.drawable.ic_push);
    // etc

But this doesn't really help me, since I won't know which Activity to open until I actually receive the push notification and parse the JSON. So, what I'm looking for is something like:

public class PushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            if(json contains id = 0) {
                // open ActivityA when the user opens this notification
            } else if(json contains id = 1) {
                // open ActivityB when the user opens this notification
            }
        } catch (JSONException e) {
            Log.d("", "JSONException: " + e.getMessage());
        }
    }
}

Any ideas how this can be accomplished using Parse? Thanks.

1

1 Answers

0
votes

This is not a direct and good solution but i think it does the work (haven't tried it myself). You can use that broadcast receiver and call PushService.setDefaultPushCallback(context, ActivityAorB.class); based on your JSON data. This will set a default activity to be called when the push message is received so that this activity will open when the user taps on the notification.

Be sure to add this receiver to your manifest.

More info about broadcast receiver on parse