Android8.0通知栏适配

目录

目录

前言

Android8.0系统对通知栏做了改动,要求实现通知必须得创建相应的channel,每个channel必须包含三个元素:channelID(渠道ID)、channelName(渠道名称)、importance(重要性),其中channelID是每个渠道的唯一标识,channelName是提供给用户看的方便用户管理,importance则代表了通知的重要性。Google这样改动主要是想方便用户对于通知信息的管理如下图。


适配Android8.0的方法

1. 首先我们先创建两个Channel

class NotifycationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notifycation)
        //先创建两个Channel
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            var channelId = "one"
            var channelName = "我是第一个"
            var importance = NotificationManager.IMPORTANCE_HIGH//重要性高
            createNotificationChannel(channelId, channelName, importance)

            channelId = "two"
            channelName = "我是第二个"
            importance = NotificationManager.IMPORTANCE_DEFAULT//重要性默认
            createNotificationChannel(channelId, channelName, importance)
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private fun createNotificationChannel(channelId: String, channelName: String, importance: Int) {
        val channel = NotificationChannel(channelId, channelName, importance)
        getNotifyCationManager().createNotificationChannel(channel)
    }

    /**
     * 获取通知管理者
     */
    private fun getNotifyCationManager(): NotificationManager {
        val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        return notificationManager
    }
}

2. 我们通过两个方法来发送通知

private fun sendOneMsg() {
        val notification = NotificationCompat.Builder(this, "one")
                .setContentTitle("One消息")//通知标题
                .setContentText("我是One消息")//通知内容
                .setWhen(System.currentTimeMillis())//显示的时间
                .setSmallIcon(R.drawable.ic_android)//小图标
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))//大图标
                .setAutoCancel(true)
                .build()
            getNotifyCationManager().notify(1, notification)
    }

    private fun sendTwoMsg() {
        val notification = NotificationCompat.Builder(this, "two")
                .setContentTitle("Two消息")
                .setContentText("我是Two消息")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_android)
                .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .build()
        getNotifyCationManager().notify(2, notification)
    }

3. 添加两个按钮调用这两个方法

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/bt_one"
        android:text="One"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_two"
        android:text="Two"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
class NotifycationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notifycation)
        ...省略部分代码
        bt_one.setOnClickListener {
            sendOneMsg()
        }
        bt_two.setOnClickListener {
            sendTwoMsg()
        }
    }
    ...省略部分代码
}

这时我们已经成功适配了Android8.0的通知栏了,这里的效果是重要性高的会出现悬浮的通知栏,而重要性低的则没有。


拓展

刚刚的效果不知道大家有没有注意到,Android8.0系统的通知栏可以进行左滑管理,我们可以屏蔽通知也可以进入更详细的通知管理。



另外我们还可以设置让它延后一定的时间再显示。


注意

如果我们把通知的小图标换成如下图片这个会怎么样呢。



换完之后我们发现通知栏的小图标并不是跟上面的图片一样而是一个灰色的方块。



其实这里你需要将小图标换成一个背景为透明的图标,才能够显示图形,比如换成下面这张图片。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。