Android - Gardez une notification régulière au Bar Notification
-
26-10-2019 - |
Question
Je l'ai écrit la fonction de notification et d'affichage à la barre de notification:
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);
}
Son beau travail, mais what way we can keep the notification steady at Notification bar
, même lorsque l'utilisateur appuie sur la "claire" bouton notificatios de la barre de notification?
S'il vous plaît jeter un oeil à la barre de notification de l'application "Yahoo".
Je suis passé par cet article SDK: http: // developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating , mais pas savoir.
La solution
Utilisez FLAG_NO_CLEAR
Il suffit de le mettre sur votre instance de Notification
avant de fournir à la NotificationManager
:
notificaton.flags |= Notification.FLAG_NO_CLEAR;
modifier: Je viens de remarquer cependant que si vous utilisez Notification.Builder
(depuis Honeycomb) et faire la notification « en cours », il deviendra également à l'abri de « effacer tout ». Voir .
appearently, ce qui est censé dissuader les développeurs d'utiliser le FLAG_NO_CLEAR
sur un notificaton non permanent car cela pourrait confondre l'utilisateur.
Autres conseils
Essayez setOngoing (true).
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)