Android开发经验谈Android技术知识Android开发

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

2019-09-27  本文已影响0人  103style

转载请以链接形式标明出处:
本文出自:103style的博客


记录一下

大致思路


伪代码

通过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);
        }
    }
}

以上

上一篇下一篇

猜你喜欢

热点阅读