0

There is one requirement where I need to show alert box with yes and no button when I receive push notification using GCM.

There will be two scnerios:

  1. If app is in background, User gets the notificaiton in titile bar. On click of notification user will be directed to app and here alert box needs to be shown where user will be having two options i.e Yes or No

  2. If app is in foreground, User automatically gets alert box needs to be shown where user will be having two options i.e Yes or No**

Please help me achieving this.

Android_Code_Chef
  • 905
  • 4
  • 13
  • 20

2 Answers2

0

Use a local event bus implementation, like LocalBroadcastManager, greenrobot's EventBus, or Square's Otto. Raise an event when the GCM message arrives. Have the UI layer register for that event when it is in the foreground, and unregister when it moves to the background. If you determine that the UI layer did not respond to the event, display the Notification.

Here are three sample apps -- one for each of the above event bus implementations -- that demonstrates this. I use AlarmManager rather than a GCM message, but the concept remains the same.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

First you need to check if your application is in background. You can call below code on onPause() on every activity in your application:

/**
* Checks if the application is being sent in the background (i.e behind
* another application's Activity).
* 
* @param context the context
* @return <code>true</code> if another application will be above this one.
*/
public static boolean isApplicationSentToBackground(final Context context) {
 ActivityManager am = (ActivityManager)        context.getSystemService(Context.ACTIVITY_SERVICE);
 List<RunningTaskInfo> tasks = am.getRunningTasks(1);
 if (!tasks.isEmpty()) {
 ComponentName topActivity = tasks.get(0).topActivity;
  if (!topActivity.getPackageName().equals(context.getPackageName())) {
  return true;
 }
}

return false;

}

If yes, then show the Notification to the user.

Add this line in your manifest file :

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

For generating Notification, you can check my answer here

Hope this helps.

Community
  • 1
  • 1
Siddharth_Vyas
  • 9,972
  • 10
  • 39
  • 69