2010-05-26 14 views
7

He tenido un problema con una notificación que no se abre/va a la actividad correcta cuando se ha hecho clic en ella.Notificaciones de la barra de estado de Android: apertura de la actividad correcta al seleccionar una notificación

Mi código de notificación (que se encuentra en una clase que se extiende de servicio):

Context context = getApplicationContext(); 

    CharSequence contentTitle = "Notification"; 

    CharSequence contentText = "New Notification"; 

    final Notification notifyDetails = 
     new Notification(R.drawable.icon, "Consider yourself notified", System.currentTimeMillis()); 

    Intent notifyIntent = new Intent(context, MainActivity.class); 

    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, 
      notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL); 

    notifyDetails.setLatestEventInfo(context, contentTitle, contentText, intent); 

    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, notifyDetails); 

Si hago clic en la notificación, mientras que la aplicación que creó el servicio está abierto, la notificación desaparece (debido a la FLAG_AUTO_CANCEL) pero el la actividad no cambia

Si hago clic en la notificación desde la pantalla de inicio, la notificación desaparece y mi aplicación se pone al frente, sin embargo permanece en la actividad que estaba abierta antes de ir a la pantalla principal, en lugar de ir a la pantalla principal.

¿Qué estoy haciendo mal? ¿Cómo especifico la actividad que se desplegará?

Respuesta

14

en realidad podría haber respondido a mi propia pregunta:

Intent notifyIntent = new Intent(Intent.ACTION_MAIN); 
notifyIntent.setClass(getApplicationContext(), Main.class); 
+0

Gracias por compartir su respuesta, ¿podría hablar de lo que contiene el "Main.class"? – OnlyHope

+0

¿Y qué es getApplicationContext()? – OnlyHope

+1

Main.class sería la actividad que desea abrir/goto. y getApplicationContext es un método en la clase Activity que se hereda de la clase ContextWrapper. http://developer.android.com/reference/android/content/ContextWrapper.html#getApplicationContext%28%29 –

Cuestiones relacionadas