zoukankan      html  css  js  c++  java
  • 适配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("测试标题", "测试内容");
    
  • 相关阅读:
    使用CSS3 will-change提高页面滚动、动画等渲染性能----------------------------引用
    JavaScript 中 console 的用法 -------------------引用
    Babel 转译 class 过程窥探--------引用
    对JS继承的研究--------------引用
    对React的研究-------------引用
    matplotlib绘制子图
    matplotlib颜色线条及绘制直线
    matplotlib示例
    爬虫流程(前面发过的文章的合集)巩固
    菜鸟教程的 mysql-connector 基础
  • 原文地址:https://www.cnblogs.com/Mr-quin/p/8584797.html
Copyright © 2011-2022 走看看