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("测试标题", "测试内容");
    
  • 相关阅读:
    selenium+phantomjs爬取bilibili
    使用 python 开发 Web Service
    OBIEE 立方刷新的问题
    解析OracleOLAP使用MView刷新Cube
    Codeforces Round #755 (Div. 2, based on Technocup 2022 Elimination Round 2)(CF1589)题解
    Codeforces Round #754 (Div. 2)(CF1605)题解
    完美解读Linux中文件系统的目录结构
    C#中获取程序当前路径的集中方法
    30个优秀.net在线学习资源站点
    如何删除windows service(转帖)
  • 原文地址:https://www.cnblogs.com/Mr-quin/p/8584797.html
Copyright © 2011-2022 走看看