Android Notification 兼容api26以上 安卓8.0以下都行

本文分享了一次重构老项目的过程,重点讲述了在Android中如何处理通知,包括创建Notification Channel,设置通知内容和行为,以及启动前台服务以保持通知的持续显示。代码示例展示了如何构建和配置Notification,以及处理不同Android版本的兼容性问题。
摘要由CSDN通过智能技术生成

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

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)
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值