Search code examples
androidandroid-activitybroadcastreceivergoogle-cloud-messaging

Activity Listener - Google Cloud Messaging - BroadcastReceiver


I have implemented GCM in my android application and it's working fine with receiving messages. The BroadcastReceiver is set up in the manifest-file according to the examples provided by Google.

My question is the following: If the user is having the application open and I want to update some results in that view - how can this be done? I was first thinking of registering this activity as a listener on whatever the BroadCastReceiver recieves. However, this must then be a static list of listeners, as a new instance of the BroadcastReceiver will be set up - but maybe this is not the way to do this.

This is what I currently have

        public class GCMBroadcastReceiver extends WakefulBroadcastReceiver  {

            @Override
            public void onReceive(Context context, Intent intent) {
                ComponentName comp = new ComponentName(context.getPackageName(),
                        GCMIntentService.class.getName());
                startWakefulService(context, (intent.setComponent(comp)));
                setResultCode(Activity.RESULT_OK);
            }
        }


        public class GCMIntentService extends IntentService {
            public static final int NOTIFICATION_ID = 1;
            private NotificationManager mNotificationManager;
            NotificationCompat.Builder builder;

            public GCMIntentService() {
                super("GCMIntentService");
            }

            @Override
            protected void onHandleIntent(Intent intent) {
                Bundle extras = intent.getExtras();
                GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
                String messageType = gcm.getMessageType(intent);

                if (!extras.isEmpty()) {
                   if (GoogleCloudMessaging.
                            MESSAGE_TYPE_MESSAGE.equals(messageType)) {

                     /** How to check if the activity 
GameActivity is running, and hence send an 
update signal to it? If it's not running a 
notification should be created.
    **/
                   }
                }
                GCMBroadcastReceiver.completeWakefulIntent(intent);
            }
        }

Here's the important part for this in the manifest-file:

       <receiver
            android:name="q.w.e.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="q.w" />
            </intent-filter>
        </receiver>

        <service android:name="q.w.e.gcm.GCMIntentService" />

Any advice?

Thanks!


Solution

  • There are two ways to handle this.

    1. Check if activity is running.

    ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;
    if(componentInfo.getPackageName().equalsIgnoreCase("com.yourpackagename")){
        //Activity Running
        Send a broadcast with the intent-filter which you register in your activity
        where you want to have the updates
    } 
    else{
        //Activity Not Running
        //Generate Notification
    }
    

    2. Using SendOrderedBroadcast.

    This blog will give you an idea on how this could be achieved.