Android清除通知栏的对应的消息

转载请以链接形式标明出处:
本文出自: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);
        }
    }
}

以上

Android通知是用来显示推送消息的重要组件之一。要在Android应用中实现通知推送消息,你需要进行以下步骤: 1. 创建通知渠道:从Android 8.0(API级别26)开始,你需要创建通知渠道来管理和组织通知。使用NotificationChannel类来创建通知渠道,并设置其名称、描述和重要性级别等参数。 2. 构建通知内容:使用NotificationCompat.Builder类来构建通知的内容,包括标题、文本、图标、大图等。 3. 设置点击行为:可以为通知设置点击行为,比如打开应用的某个界面或执行特定的操作。使用PendingIntent类来定义点击通知时要执行的动作。 4. 发送通知:通过NotificationManager类的notify()方法发送通知指定一个唯一的通知ID以及之前创建的NotificationCompat.Builder对象。 下面是一个示例代码,演示了如何发送一个简单的通知: ```java // 创建通知渠道 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT); channel.setDescription("Channel Description"); // 在NotificationManager中创建通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } // 构建通知内容 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Notification Title") .setContentText("Notification Text") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(pendingIntent) // 设置点击行为 .setAutoCancel(true); // 点击后自动取消通知 // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值