overview open notification from example custom anatomy activity android notifications android-intent

android - open - getIntent() Extras siempre NULL



notifications android example (5)

Como parece que su actividad ya se está ejecutando, creo que necesita especificar FLAG_UPDATE_CURRENT ; de lo contrario, la llamada getIntent() devolverá la anterior. Vea esta respuesta .

Escribí una sencilla aplicación de Android que muestra una notificación personalizada como esta:

Context context = getApplicationContext(); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification( R.drawable.icon, title, System.currentTimeMillis()); Intent notificationIntent = new Intent( context, this.getClass()); notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE"); PendingIntent pendingIntent = PendingIntent.getActivity( context , 0, notificationIntent, 0); notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT; notification.contentView = new RemoteViews(context.getPackageName(), R.layout.notifypbar); notification.contentIntent = pendingIntent; notification.contentView.setTextViewText(R.id.notifypb_status_text, text); notification.contentView.setProgressBar(R.id.notifypb_status_progress, 100, (int)(100*progress), false); manager.notify(104, notification);

Este fragmento de código se llama SOLO UNA VEZ en mi aplicación y muestra una notificación con una barra de progreso (todo correctamente).

Ahora, cuando un usuario hace clic en esta notificación, mi aplicación maneja el evento onResume .

public void onResume() { super.onResume(); // TODO: Extras è SEMPRE NULL!!! impossibile! Intent callingintent = getIntent(); Bundle extras = callingintent.getExtras();

¡pero los extras siempre son NULOS!

Intenté cualquier combinación de:

notificationIntent.putExtra("com.mysecure.lastpage", "SECURECODE");

o

Bundle extra = new Bundle(); extra.putString(key, value); notificationIntent.putExtra(extra);

pero getIntent (). getExtras () devuelve siempre NULL.


Este es el escenario:
El método getIntent() devuelve la PRIMERA intención que la actividad de inicio.

Entonces, cuando la actividad está CERRADA (terminada) y el usuario hace clic en la notificación, ejecutará una nueva instancia de la actividad y getIntent() funciona como se espera (Extras no es null ).

Pero si la actividad está "durmiendo" (está en el fondo) y el usuario hace clic en la notificación, getIntent() siempre devuelve el PRIMER intento que inició la actividad y NO el intento de notificación.

Para captar el intento de notificación mientras la aplicación se está ejecutando, simplemente use esto

notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

y luego anula onNewIntent(Intent newintent) .

Por lo tanto, cuando una aplicación se ejecuta por primera vez, se puede usar getIntent() y cuando la aplicación se reanuda desde la onNewIntent , onNewIntent funciona.



Simplemente escriba este código encima de su sobre su método Resume (). Esto es todo lo que se necesita. Esto refresca la intención, realmente no lo sé, pero funciona.

@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); }


Problema: está enviando el mismo código de solicitud para su intens pendiente. Cambia esto.

Solución : configure la variable global int UNIQUE_INT_PER_CALL = 0 y cuando cree una llamada pendiente pendiente como a continuación.

PendingIntent contentIntent = PendingIntent.getActivity(context, UNIQUE_INT_PER_CALL, notificationIntent, 0);

UNIQUE_INT_PER_CALL ++; // para incrementar