2010-04-13 21 views
112

Se avuto alcuni problemi con una notifica voglio mostrare nella barra di notifica. Sebbene abbia impostato il flag di notifica su Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL, la notifica non scompare dopo aver fatto clic su di essa. Qualche idea su cosa sto facendo male?notifica Android non scompare dopo aver cliccato il notifica

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.icon; 
    CharSequence tickerText = "Ticker Text"; 
    long time = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, time); 
    notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Title"; 
    CharSequence contentText = "Text"; 
    Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    mNotificationManager.notify(1,notification); 

risposta

247

Mentre la costruzione Notification da NotificationBuilder è possibile utilizzare notificationBuilder.setAutoCancel(true);.

+0

Grazie mille. Ha funzionato per me – Sakthimuthiah

+2

Quindi, quali differenze creano notifiche usando Notification 'mNotificationManager.notify (1, notification);' e usando NotificationBuilder 'mNotificationManager.notify (1, mBuilder.build());'? Grazie. – NPE

+9

Questa risposta dovrebbe essere accettata, è più in linea con la dottrina corrente di progettazione Android – jmaculate

126
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL 

Dalla documentazione:

po 'a bitwise- o ndr nel campo flag che deve essere impostato se la notifica deve essere annullato quando viene cliccato dal utente

+0

Oh uomo, grazie! La prossima volta leggerò la documentazione più correttamente. – Flo

+3

Questa non è la risposta corretta. 'Notification.DEFAULT_LIGHTS' fa parte della classe' Notification.defaults', non la classe 'Notification.flags'. Vedi la mia risposta per i setter appropriati. – Darcy

+0

Grazie amico, mi ha aiutato –

27
// Uses the default lighting scheme 
notification.defaults |= Notification.DEFAULT_LIGHTS; 

// Will show lights and make the notification disappear when the presses it 
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
+1

Ho esaminato i documenti di Android. Non riesco a capire quando devono essere usate le bandiere. Perché non solo notification.defaults = notification.DEFAULT_LIGHTS è sufficiente per mostrare le luci. Perché la vibrazione e il suono funzionano senza la bandiera. – Ashwin

+0

Sto usando il NotificationBuilder, NotificationCompat.Builder mBuilder = new NotificationCompat.Builder (questo) .setSmallIcon (android.R.drawable.ic_popup_sync) .setContentTitle ("Nuovo Tweet") .setContentText ("Non ci sono" + count + "tweet"); mBuilder.setDefaults (NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); – Joseph

0

utilizzare il flag Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); 

notification.setLatestEventInfo (contesto, contentTitle, contentText, pendingIntent);

// Annulla la notifica dopo il suo selezionato notification.flags | = Notification.FLAG_AUTO_CANCEL; e per avviare l'app:

NotificationManager notificationManager = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(context, App.class); 

PendingIntent pendingIntent = PendingIntent.getActivity (contesto, intent_id, intento, PendingIntent.FLAG_UPDATE_CURRENT);

Problemi correlati