IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 移动开发 -> Android Notification 兼容api26以上 安卓8.0以下都行 -> 正文阅读

[移动开发]Android Notification 兼容api26以上 安卓8.0以下都行

老树发新芽。最近重构了一老项目,真是障碍重重啊

Notification

直接CV 就能用 , 因为时间有限 ,以下代码是从自己的项目中截取的,有很多配置只适合我自己的项目,如果问题留言沟通


    val NOTIFICATION_FLAG = 0X11
    lateinit var mNotificationManager: NotificationManager
    lateinit var builder: Notification.Builder

    private fun initNotification() {
        // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。
        mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        //构建一个Notification构造器
        builder = Notification.Builder(this.applicationContext)
        //  兼容代码
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val CHANNEL_ONE_ID = "com.zhf"
            val CHANNEL_ONE_NAME = "Channel ONE"
            // 改方法是 Android 8.0 以后才有的
            builder.setChannelId(CHANNEL_ONE_ID)
            
            var notificationChannel = NotificationChannel(
                CHANNEL_ONE_ID,
                CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH
            )
            notificationChannel.enableLights(false)
            notificationChannel.setSound(null, null)
            notificationChannel.lightColor = Color.RED
            notificationChannel.setShowBadge(true)
            notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
            mNotificationManager.createNotificationChannel(notificationChannel)
        }

        // 设置点击通知跳转的Intent  延迟Intent
        // 最后一个参数可以为PendingIntent.FLAG_CANCEL_CURRENT 或者 PendingIntent.FLAG_UPDATE_CURRENT
        val pendingIntent =
            PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), 0)

        builder.setContentIntent(pendingIntent) // 设置点击跳转界面
            .setTicker("您有一个notification") // statusBar上的提示
            .setContentTitle("工作待命状态") // 设置下拉列表里的标题
            .setSmallIcon(R.mipmap.logo) // 设置状态栏内的小图标24X24
            .setContentText("休息时间") // 设置详细内容
            .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间
            //                .setDefaults(Notification.DEFAULT_VIBRATE) //默认震动方式
            .setPriority(Notification.PRIORITY_HIGH) //优先级高
            .setAutoCancel(false)

        val notification: Notification = builder.build() // 获取构建好的Notification
        //        notification.defaults = Notification.DEFAULT_SOUND; //设置为默认的声音

        notification.flags = notification.flags or
                Notification.FLAG_AUTO_CANCEL or  // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
                NotificationCompat.FLAG_ONGOING_EVENT or//将此通知放到通知栏的"Ongoing"即"正在运行"组中
                NotificationCompat.FLAG_NO_CLEAR //表明在点击了通知栏中的"清除通知"后,此通知不清除,常与FLAG_ONGOING_EVENT一起使用
        mNotificationManager.notify(NOTIFICATION_FLAG, notification)

        // 启动前台服务
        // 参数一:唯一的通知标识;参数二:通知消息。
        startForeground(NOTIFICATION_FLAG, notification) // 开始前台服务
    }

    fun addMyNotification() {
        builder.setContentTitle(getTitle())
            .setContentText(getText())
            .setAutoCancel(false)
        val notification = builder.build() 
        
        notification.flags = notification.flags or
                Notification.FLAG_AUTO_CANCEL or 
                NotificationCompat.FLAG_ONGOING_EVENT or
                NotificationCompat.FLAG_NO_CLEAR 
        mNotificationManager.notify(NOTIFICATION_FLAG, notification)
    }

  移动开发 最新文章
Vue3装载axios和element-ui
android adb cmd
【xcode】Xcode常用快捷键与技巧
Android开发中的线程池使用
Java 和 Android 的 Base64
Android 测试文字编码格式
微信小程序支付
安卓权限记录
知乎之自动养号
【Android Jetpack】DataStore
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 12:12:20  更:2021-08-07 12:14:28 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 14:45:31-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码