Android - Mantenere la notifica stabile a barra di notifica
-
26-10-2019 - |
Domanda
ho scritto la funzione di notifica e mostra a barra di notifica:
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);
}
Il suo funzionamento benissimo, ma what way we can keep the notification steady at Notification bar
, anche quando l'utente preme il "chiaro" pulsante dalla barra di notifica notificatios?
Si prega di dare un'occhiata al bar "Yahoo" dell'applicazione di notifica.
Ho passato con questo articolo SDK: http: // developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating , ma non è riuscito a scoprire.
Soluzione
Usa FLAG_NO_CLEAR
Basta impostare sul vostro esempio Notification
prima di fornire al NotificationManager
:
notificaton.flags |= Notification.FLAG_NO_CLEAR;
modifica: Ho appena notato però che se si sta utilizzando Notification.Builder
(dal nido d'ape) ed effettuare la notifica "in corso", ma sarà anche diventare immune a "Cancella tutto". Vedere qui .
appearently, questo dovrebbe scoraggiare gli sviluppatori di utilizzare il FLAG_NO_CLEAR
su un notificaton non in corso dal momento che questo potrebbe confondere l'utente.
Altri suggerimenti
Prova 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)