• 适配Android O的通知


    创建类

    public class NotificationUtils extends ContextWrapper {
    
        private NotificationManager manager;
        public static final String id = "channel_1";
        public static final String name = "channel_name_1";
    
        public NotificationUtils(Context context){
            super(context);
        }
    
        public void createNotificationChannel(){
            NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
            getManager().createNotificationChannel(channel);
        }
        private NotificationManager getManager(){
            if (manager == null){
                manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            }
            return manager;
        }
        public Notification.Builder getChannelNotification(String title, String content){
            return new Notification.Builder(getApplicationContext(), id)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(android.R.drawable.stat_notify_more)
                    .setAutoCancel(true);
        }
        public NotificationCompat.Builder getNotification_25(String title, String content){
            return new NotificationCompat.Builder(getApplicationContext())
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(android.R.drawable.stat_notify_more)
                    .setAutoCancel(true);
        }
        public void sendNotification(String title, String content){
            if (Build.VERSION.SDK_INT>=26){                                                   //api>=26时
                createNotificationChannel();
                Notification notification = getChannelNotification
                        (title, content).build();
                getManager().notify(1,notification);
            }else{
                Notification notification = getNotification_25(title, content).build();
                getManager().notify(1,notification);
            }
        }
    }
    

    使用

    NotificationUtils notificationUtils = new NotificationUtils(view.getContext().getApplicationContext());
    notificationUtils.sendNotification("测试标题", "测试内容");
    
  • 相关阅读:
    浅析数据库安全技术
    本站快捷付款方式
    VMware Workstation 官方正式版及激活密钥
    Win10真正好用之处
    我眼中的CentOS 下 安全策略
    美团
    Tomcat connector元素常用配置(最大连接数等)
    9.22面经:
    9.7
    合并两个有序数组为一个新的有序数组
  • 原文地址:https://www.cnblogs.com/Mr-quin/p/8584797.html
Copyright © 2020-2023  润新知