Android: mantenga la notificación estable en la barra de notificaciones
-
26-10-2019 - |
Pregunta
He escrito la función para notificar y mostrar en la barra de notificaciones:
private void showNotification()
{
CharSequence title = "Hello";
CharSequence message = "Notification Demo";
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "A Notification", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, Main_Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(Main_Activity.this, title, message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
}
Está funcionando bien, pero what way we can keep the notification steady at Notification bar
¿Incluso cuando el usuario presiona el botón "Borrar" Notificatios desde la barra de notificaciones?
Eche un vistazo a la barra de notificación de la aplicación "Yahoo".
He pasado por este artículo de SDK: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#updating , pero no se enteró.
Solución
Usar FLAG_NO_CLEAR
Solo póntalo en tu Notification
instancia antes de suministrarlo al NotificationManager
:
notificaton.flags |= Notification.FLAG_NO_CLEAR;
editar: Sin embargo, acabo de notar que si estás usando Notification.Builder
(Desde el panal) y hacer que la notificación sea "en curso", también se volverá inmune a "despejar todo". Ver aquí.
Aparentemente, se supone que esto desalienta a los desarrolladores de usar el FLAG_NO_CLEAR
en un Notificaton que no era de girar, ya que esto podría confundir al usuario.
Otros consejos
Probar Setongoing (verdadero).
notification = new Notification.Builder(getApplicationContext())
.setLargeIcon(
BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher).setTicker(tickerText)
.setWhen(System.currentTimeMillis()).setContentTitle(contentTitle)
.setOngoing(true)
.setContentText(contentText)
.setContentIntent(contentIntent)