Android中的3种通知Notification

Android中的Notification分为三种:

       1、普通Notification

       2、折叠Notification

       3、悬浮Notification

       1、普通Notification

   //普通通知
            val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/qq_40716430/article/details/105690486"))
            val pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0)
            val builder = NotificationCompat.Builder(this, "AppTestNotificationId")
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    .setContentTitle("普通通知")
                    .build()
//            //添加如下代码,android版本需要手动添加NotificationChannel实现
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationChannel = NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT)
                nm!!.createNotificationChannel(notificationChannel)
            }
            nm!!.notify(1, builder)

效果如下:

 

2、折叠Notification

 /**
            *
            *折叠式通知
            */
            val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/qq_40716430/article/details/105690486"))
            val pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0)
            val remoteViews = RemoteViews(packageName, R.layout.view_fold)
            val builder = NotificationCompat.Builder(this, "AppTestNotificationId")
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    .setContentTitle("折叠式通知")

            val notification = builder.build()
            //指定展开时的视图
            notification.bigContentView=remoteViews
            //添加如下代码,android版本需要手动添加NotificationChannel实现
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val notificationChannel = NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT)
                nm!!.createNotificationChannel(notificationChannel)
            }
            nm!!.notify(1, notification)

效果如下:

 

   3、悬浮Notification

 /**
             *
             *悬挂式通知
             * 如果不显示,看看App悬浮通知选项是否打开
             */
            val mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://blog.csdn.net/qq_40716430/article/details/105690486"))
            val pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0)
            val builder = NotificationCompat.Builder(this, "AppTestNotificationId")
                    .setContentIntent(pendingIntent)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                    .setAutoCancel(true)
                    .setContentTitle("悬挂式通知")

            //设置点击跳转
            val hangIntent = Intent()
            hangIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            hangIntent.setClass(this@MainActivity, MainActivity2::class.java)
            val activity = PendingIntent.getActivity(this@MainActivity, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT)
            builder.setFullScreenIntent(activity, true)
            val notification = builder.build()
            nm!!.notify(2, notification)

效果如下:

关注公众号,不迷路喔

 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值