点击notifcation后,Android的通知不会消失通知、notifcation、Android

2023-09-03 20:33:14 作者:庸庸碌碌

如果有一个通知,我想在通知栏显示的一些问题。虽然我设置通知标志 Notification.DEFAULT_LIGHTS和放大器; Notification.FLAG_AUTO_CANCEL 的通知不点击它后消失。任何想法,我做错了什么?

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

    INT图标= R.drawable.icon;
    CharSequence的tickerText =新浪体育讯北京时间文本;
    很长一段时间= System.currentTimeMillis的();

    通知通知=新的通知(图标,tickerText,时间);
    notification.flags = Notification.DEFAULT_LIGHTS和放大器; Notification.FLAG_AUTO_CANCEL;

    上下文的背景下= getApplicationContext();
    CharSequence的contentTitle =标题;
    CharSequence的contentText =文本;
    意图notificationIntent =新的意图(这一点,SilentFlipConfiguration.class);
    PendingIntent contentIntent = PendingIntent.getActivity(此,0,notificationIntent,0);
    notification.setLatestEventInfo(背景下,contentTitle,contentText,contentIntent);
    mNotificationManager.notify(1,通知);
 

解决方案

  notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
 
Nevolution让你的Android通知更好用 在通知栏就能回复微信消息

从文档:

  

位被bitwise- 或编入,应设置标志字段   如果通知应取消当由用户点击

If got some issues with a notification I want to show in the notification bar. Although I set the notification flag to Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL the notification doesn't disappear after clicking it. Any ideas what I'm doing wrong?

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

解决方案

notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

From the documentation:

Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user