打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通知未被驳回(Android)

如果单击“操作”,则通知setAutoCancel(true)不起作用

我有一个通知,其中包含一个动作.当我点击通知时,它会从列表中删除.但是,当我单击Action它成功完成Action(即调用)时,但当我返回到通知列表时,它仍然存在.
AlarmReceiver的相对代码:

public class AlarmReceiver extends BroadcastReceiver {Meeting meeting;/** * Handle received notifications about meetings that are going to start */@Overridepublic void onReceive(Context context, Intent intent) {    // Get extras from the notification intent    Bundle extras = intent.getExtras();    this.meeting = extras.getParcelable("MeetingParcel");    // build notification pending intent to go to the details page when click on the body of the notification    Intent notificationIntent = new Intent(context, MeetingDetails.class);    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    notificationIntent.putExtra("MeetingParcel", meeting);      // send meeting that we received to the MeetingDetails class    notificationIntent.putExtra("notificationIntent", true);    // flag to know where the details screen is opening from    PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);    // build intents for the call now button    Intent phoneCall = Call._callIntent(meeting);    if (phoneCall != null) {        PendingIntent phoneCallIntent = PendingIntent.getActivity(context, 0, phoneCall, PendingIntent.FLAG_CANCEL_CURRENT);        int flags = Notification.FLAG_AUTO_CANCEL;        // build notification object        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);        Notification notification = builder.setContentTitle("Call In")                .setContentText(intent.getStringExtra("contextText"))                .setTicker("Call In Notification")                .setColor(ContextCompat.getColor(context, R.color.colorBluePrimary))                .setAutoCancel(true)                    // will remove notification from the status bar once is clicked                .setDefaults(Notification.DEFAULT_ALL)  // Default vibration, default sound, default LED: requires VIBRATE permission                .setSmallIcon(R.drawable.icon_notifications)                .setStyle(new NotificationCompat.BigTextStyle()                        .bigText(meeting.description))                .addAction(R.drawable.icon_device, "Call Now", phoneCallIntent)                .setCategory(Notification.CATEGORY_EVENT)   // handle notification as a calendar event                .setPriority(Notification.PRIORITY_HIGH)    // this will show the notification floating. Priority is high because it is a time sensitive notification                .setContentIntent(pIntent).build();        notification.flags = flags;        // tell the notification manager to notify the user with our custom notification        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        notificationManager.notify(0, notification);    }  }}

解决方法:

我今天遇到了这个问题,发现FLAG_AUTO_CANCEL和setAutoCanel(true)都在点击通知时起作用,
但不适合行动点击

简单地说,在目标服务或行动活动中,取消通知

NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);manager.cancelAll();

或者如果有更多通知

manager.cancel(notificationId);
来源:https://www.icode9.com/content-4-316951.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
android Notification 的使用
Android Notification通知
2.5.8 Notification(状态栏通知)详解 | 菜鸟教程
Android Notification 详解
Android Intent和PendingIntent的区别详细分析
pendingIntent简单理解
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服