Mostrar cuadro de alerta en la notificación de GCM recibida

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**

Por favor, ayúdame a lograr esto.

preguntado el 28 de mayo de 14 a las 12:05

I already done this in my Current App. -

@Simple Plan: Can you please share me code for the same? -

@ACC directly asking about CODE? ha first post your code what you tried till now? -

@SimplePlan I am also facing similar problem I already asked a question here please guide me if you have fixed it… -

2 Respuestas

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.

contestado el 28 de mayo de 14 a las 12:05

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 aquí

Espero que esto ayude.

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.