Android清除通知栏的对应的消息
2023-02-18 16:34:03 时间
转载请以链接形式标明出处: 本文出自:103style的博客
记录一下
大致思路
- 我们收到推送消息的时候会通过
NotificationManager.notify(int id, Notification notification)
发送到通知栏。 - 记录每一个显示的 通知栏消息 和 对应的
id
. - 按产品要求在进入对应的页面的时候通过
NotificationManager.cancel(id)
删除对应的通知栏消息。
伪代码
通过sendNotification(...)
显示推送消息,在对应的界面调用类似 cleanMsgNotify(int notice)
清除推送消息即可。
public static final String CHANNEL_ID = "XXXX";
private static NotificationManager mNotificationManager;
private static List<PushMessageBean> notifyList;
public synchronized static void cleanMsgNotify(int notice) {
if (mNotificationManager == null
|| notifyList == null || notifyList.size() == 0) {
return;
}
for (int i = notifyList.size() - 1; i >= 0; i--) {
PushMessageBean t = notifyList.get(i);
if (t.notice == notice) {
mNotificationManager.cancel(t.notifyId);
notifyList.remove(i);
}
}
}
public void sendNotification(Context context, PushMessageBean message) {
if (TextUtils.isEmpty(message.message)) {
return;
}
NotificationCompat.Builder mBuilder;
int notifyId = (int) System.currentTimeMillis();
if (mNotificationManager == null) {
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
}
registerNotificationChannel();
mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID);
mBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
.setAutoCancel(true)
.setContentText(message.message)
.setSmallIcon(R.drawable.ic_launchers_round)
.setVibrate(new long[]{1000})
.setColor(context.getResources().getColor(R.color.color_primary))
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message.message));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {//7.0以上不需要title
mBuilder.setContentTitle(context.getResources().getString(R.string.app_name));
}
message.notifyId = notifyId;
saveNotification(message);
mNotificationManager.notify(notifyId, mBuilder.build());
}
private void saveNotification(PushMessageBean message) {
if (notifyList == null) {
notifyList = new ArrayList<>();
}
notifyList.add(message);
}
private void registerNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = mNotificationManager.getNotificationChannel(CHANNEL_ID);
if (notificationChannel == null) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_ID, NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true); //是否在桌面icon右上角展示小红点
channel.setLightColor(Color.RED); //小红点颜色
//channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
mNotificationManager.createNotificationChannel(channel);
}
}
}
以上
相关文章
- 小程序|炎炎夏日、清爽一夏、头像大换装
- Mac下安装NDK(android开发)
- textview设置字体大小-Android应用开发之TextView设置个别字体样式
- java小程序微信支付与提现
- iphone没有sim卡能用吗-升级ios15无sim卡怎么办
- MacDroid for mac(安卓设备文件传输助手) v1.6.1中文版
- 使用JQuery和bootstrap-select实现世界城市的三级联动(中英互译)
- AnyMP4 iOS Cleaner for mac(IOS文件清理工具)v1.0.16激活版
- 药物互助小程序的120个小时
- Android平台GB28181设备接入端如何实时更新经纬度实现国标平台侧电子地图位置标注
- Android 子线程 UI 操作真的不可以?
- 移动安全-安卓frida hook基础篇
- 微信开放Donut,我们的App也能像微信跑小程序了
- Android Activity Deeplink启动来源获取源码分析
- Android构建工具--AAPT2源码解析(一)
- Android模块化开发实践
- 手把手教你实现Android编译期注解
- 打通“任督二脉”:Android 应用安装优化实战
- Android系统Bitmap内存分配原理与优化
- Android客户端网络预连接优化机制探究