2010-04-13 2 views
112

Если у вас возникли проблемы с уведомлением, которое я хочу показать в панели уведомлений. Хотя я установил флаг уведомления Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL, уведомление не исчезает после нажатия на него. Любые идеи, что я делаю неправильно?Уведомление об этом не исчезает после нажатия на значок

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); 

ответ

247

При строительстве Notification на NotificationBuilder вы можете использовать notificationBuilder.setAutoCancel(true);.

+0

Большое спасибо. Это сработало для меня – Sakthimuthiah

+2

Итак, какие отличия создают уведомление, используя Notification 'mNotificationManager.notify (1, уведомление);' и используя NotificationBuilder 'mNotificationManager.notify (1, mBuilder.build());'? Благодарю. – NPE

+9

Этот ответ должен быть принят, он больше соответствует текущей доктрине проектирования Android. – jmaculate

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

Из документации:

бита, чтобы быть bitwise- или-й издом в поле флагов, которые должны быть установлены , если уведомление должно быть отменено, если он выполнен щелчком пользователь

+0

О, мужчина, спасибо! В следующий раз я буду читать документацию более правильно. – Flo

+3

Это неправильный ответ. 'Notification.DEFAULT_LIGHTS' является частью класса Notification.defaults, а не класса' Notification.flags'. См. Мой ответ для соответствующих сеттеров. – Darcy

+0

Спасибо, человек помог мне –

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

Я прошел через андроидные документы. Я не совсем понимаю, когда нужно использовать флаги. Почему не просто notification.defaults = notification.DEFAULT_LIGHTS достаточно, чтобы показать свет. Потому что вибрация и звук работают без флага. – Ashwin

+0

Am с помощью NotificationBuilder, NotificationCompat.Builder mBuilder = новый NotificationCompat.Builder (это) .setSmallIcon (android.R.drawable.ic_popup_sync) .setContentTitle ("Новый Tweet") .setContentText ("Есть" + подсчет + «твиты»); mBuilder.setDefaults (NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); – Joseph

0

Используйте флаг Notification.FLAG_AUTO_CANCEL

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

notification.setLatestEventInfo (контекст, contentTitle, contentText, pendingIntent);

// Отменить уведомление после его выбора notification.flags | = Notification.FLAG_AUTO_CANCEL; и для запуска приложения:

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

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

PendingIntent pendingIntent = PendingIntent.getActivity (контекст, intent_id, намерение, PendingIntent.FLAG_UPDATE_CURRENT);

Смежные вопросы