Alfonso_MA
Alfonso_MA

Reputation: 555

Do not show notification if certain activity is being displayed. Execute this activity's method instead

I have downloaded GCM example code from https://code.google.com/p/gcm/ and It works fine.

I have added two activities and now my project contais the following files:

In the original code when the gcm message arrives, the notification is always shown but now I would like the notification only be shown when the ActivityListView activity is not being displayed.

If the app is displaying ActivityListView, the notification should not be shown and an ActivityListView method should be executed instead.

I mean I need some code like this:

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    if(ActivityListView is being displayed){
        ActivityListView.mymethod(extras);
    }
    else{
        sendNotification("Received: " + extras.toString());
    }
}

Now GcmIntentService looks like:

public class GcmIntentService extends IntentService {
.....

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
        .......
            sendNotification("Received: " + extras.toString());
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

And GcmBroadcastReceiver looks like:

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);
}
}

Upvotes: 1

Views: 1241

Answers (2)

shkschneider
shkschneider

Reputation: 18253

Activity

You need first to know if your activity is displayed or not. That is not supported natively by Android. Use a simple boolean set to true in onResume() and false in onPause() should du the trick.

public class MyActivity extends Activity {

  private boolean visible;

  @Override
  public void onResume() {
    visible = true;
  }

  @Override
  public void onPause() {
    visible = false;
  }

  public boolean isVisible() {
    return visible;
  }

}

ActivityListView

Depending on your architecture, you can do many things.

I would probably create a custom broadcast intentfilter and register this like so in your Activity:

registerReceiver(new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    if (isVisible() {
      // ...
    }
    else {
      // notify
    }
  }
}, new IntentFilter("CUSTOM_BROADCAST_INTENT_FILTER"));

GcmBroadcastReceiver

That CUSTOM_BROADCAST_INTENT_FILTER broadcast would be launched from your GsmBroadcastReceiver.

sendBroadcast(new Intent("CUSTOM_BROADCAST_INTENT_FILTER"));

Upvotes: 0

Smokez
Smokez

Reputation: 382

A pretty easy way to do this is to get the current task from the ActivityManager and simply compare class names:

protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();

    ActivityManager am = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
    // Get info from the currently active task
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks( 1 );
    String activityName = taskInfo.get( 0 ).topActivity.getClassName();
    if(activityName.equals( ActivityListView.class.getName() ))
    {
        // Execute that special method from ActivityListView
        ActivityListView.mymethod(extras);
    }
    else
    {
        // Show the notification
        sendNotification("Received: " + extras.toString());
    }
}

In your AndroidManifest.xml you will need the permission:

<uses-permission android:name="android.permission.GET_TASKS" />

Upvotes: 0

Related Questions