2

I have received the GCM push Message.

How to decide whether I have to update the UI or post a notification.

If our app is in foreground then we can update the UI. If our app is not running then I need to post a notification. Whether this is the right way. Or else any other way to handle it. And how can I find that my app is in foreground or background.

Thanks in advance.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55
  • http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service check this once.its helpfull to you. – RamBabu Pudari Jan 14 '15 at 07:03
  • Is it a duplicate of http://stackoverflow.com/questions/7191413/show-a-notification-only-if-activity-is-not-showing? – Pankaj Kumar Jan 14 '15 at 07:07
  • Is not same. In GCM we have different implementation. We will get the push message inside of onHandleIntent(). How to handle the same using handler to update the UI when the app is in forground. or post notification when the app is in background. Please refer this new GCM reference document - https://developer.android.com/google/gcm/client.html which I followed. – M Vignesh Jan 14 '15 at 07:20
  • Nothing is different as I think. You need to be clear when you use such components, and how they works. – Pankaj Kumar Jan 14 '15 at 07:43
  • got clue from your answer and defined my answer below. Thanks Pankaj Kumar. – M Vignesh Jan 19 '15 at 13:17

2 Answers2

3

If you followed that guide at Android Dev (specifically, this section), your app should be posting the notification regardless of whether it is in the foreground or not.

However, if you want to change the UI of the Activity which is in the foreground, modify the PendingIntent inside the sendNotification() method to launch your Activity. You may attach extras in the associated Intent. If your Activity is in the background, it will be started and the extras will be available via getIntent() inside the Activity's lifecycle methods. If it is in the foreground, the Activity's onNewIntent() method will be called, from where you can get your extras again (that you sent from the notification).

Kedar Paranjape
  • 1,822
  • 2
  • 22
  • 33
2

Yes I found the answer by myself.

There are 3 different your current activity status.

  1. Resumed - Static reference is available
  2. Stopped - Static reference is available
  3. Destroyed - Static references are garbaged
//Define static variable of your activity instance
public static FleetLocActivity mFleetLocActivity = null;

And write the below code in your GCMIntentService. This class is called whenever there is a new push message.

if (FleetLocActivity.mFleetLocActivity != null) {
**//Activity is in Stopped or Resumed State**
        handlePushMessage(pushMessage);
        // Start Service and Update UI with the help of Handler
} else {
**//Activity is in Destroyed State**
       // Post notification of received message. And Add the action of opening your home activity. When the user clicks the notification it will open the home activity and start the respective service by using activity instance. 
       mAppUtilInstance.postGcmCommandNotification(
                 "Command Received : " + pushMessage, mContext);
}


//Class variable updated with the received push message
private String pushMessage = "";

private void handlePushMessage(String pushMsg){
   Message msgObj = gcmCommandHandler.obtainMessage();
   Bundle bundle = new Bundle();
   bundle.putString(Constants.getInstance().GCM_SERVER_MESSAGE,pushMessage);
   msgObj.setData(bundle);
   gcmCommandHandler.sendMessage(msgObj);
}


/**
 * to handle the GCM push message (or) commands using Handler
 */
private Handler gcmCommandHandler = new Handler() {
    // Create handleMessage function
    public void handleMessage(Message message) {
        String pushMessage = message.getData().getString(
                Constants.getInstance().GCM_SERVER_MESSAGE);
        if (pushMessage != null && pushMessage.length() > 0) {
            //Start Service and update UI HERE
        }
    }
};
M Vignesh
  • 1,586
  • 2
  • 18
  • 55