Friday, December 4, 2015

Android Check If App is in Foreground on Receiving GCM message

Recently in one of my project we got a requirement to do some tasks when there is a GCM message in the app. Basically the requirement was to forcefully show some screens when there is a PUSH message. So if app is in Foreground we shall directly go to screen and if app is not running and GCM is received, app should go to particular screen on start. In this blog I am going to explain how to do this.

Basically I have used ordered broadcast to achieve this. Here is step by step process.

First let's capture this when application is in foreground. First add a broadcast receiver in your activity.


BroadcastReceiver mBroadcastReceiver

Now initialize it in onCreate method of your activity.

mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            //Right here do what you want in your activity
        Log.v("message","got push on foreground");
            abortBroadcast();
        }
    };

Now register it in onResume method of your activity.

@Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("com.your_package.GOT_PUSH");
        filter.setPriority(2);
        registerReceiver(mBroadcastReceiver, filter);
}

As you can see above we register intent with action. Now go to onMessage method of your GCMIntentService and add following code.

Intent responseIntent = new Intent("com.your_package.GOT_PUSH");
sendOrderedBroadcast(responseIntent, null);

As you can see above we are sending ordered broadcast in onMessage method. So whenever there is a PUSH message app onReceive method on receiver will be called in your activity and then you can do rest of the stuff.

Now let's see what to do when app is not running or is in background. We will define a receiver for that in our manifest file.

<receiver android:name=".BackgroundGCMBroadCastReceiver">
    <ntent-filter
        android:priority="1">
        <action
            android:name="com.your_package.GOT_PUSH" />
    </intent-filter>
</receiver>

Now add a class in your Project with name BackgroundGCMBroadCastReceiver which extends BroadcastReceiver.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;

public class BackgroundGCMBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //In this receiver just send your notification
    Log.v("message","got push on background");
    }
}

That's it now you can do your logic in onReceive method. Do not forget to unRegister receiver in your onPause method of activity.

@Override
protected void onPause() {

super.onPause();
try {
unregisterReceiver(mBroadcastReceiver);
} catch (IllegalArgumentException ex) {

}
}

That's it. Hope this helps you.

No comments:

Post a Comment