8.0Android开发Android开发经验谈

Android Oreo 通知新特性,这坑老夫先踩了

2018-01-14  本文已影响647人  kindle巴巴

前些天性致脖脖地点进入了developer.android.com,想看下通知这块内容,你懂的。首先映入眼帘的就是下面这玩意儿,翻译速度阔以哦!!!

image

通知渠道?啥玩意儿,啊,走过路过,千万不要错过啊,点进去瞧瞧呗。看到代码,必须先敲完运行下啊。什么情况,Api 26上根本就出不来什么通知啊。

image

心想谷歌还是严谨的,就选择相信了它。于是我打开了英文版。

image

通知渠道

先看什么是通知渠道。来看下官方解释:

Notification channels: Android 8.0 introduces notification channels that allow you to create a user-customizable channel for each type of notification you want to display.

啥意思?就是说这个面包我不想一个一个做了,做一个模子,把面粉放进去,就行了,形象吧。

▲图片偷自于网上

没错,这些就是通知渠道。

image

我们可以看出,每个通知渠道都有一个名称,进去之后还有很多其他属性,如重要程序、通知圆点、闪烁灯等等。喂喂,那位拿8.0以下的手机试的同学,给我出去!

那有的同学要问了,怎样才能在App中显示一个通知渠道呢。问得好,我们直接上代码。

/**
 * Oreo不用Priority了,用importance
 * IMPORTANCE_NONE 关闭通知
 * IMPORTANCE_MIN 开启通知,不会弹出,但没有提示音,状态栏中无显示
 * IMPORTANCE_LOW 开启通知,不会弹出,不发出提示音,状态栏中显示
 * IMPORTANCE_DEFAULT 开启通知,不会弹出,发出提示音,状态栏中显示
 * IMPORTANCE_HIGH 开启通知,会弹出,发出提示音,状态栏中显示
 */
val channel = NotificationChannel("渠道ID",
        "测试渠道名称",
         NotificationManager.IMPORTANCE_HIGH)
// 获取NotificationManager
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) 
as NotificationManager
// 创建通知渠道
notificationManager.createNotificationChannel(channel)

就这么简单,对,就是这么简单暴力!

image

那如果想修改渠道属性怎么办呢?还是上面的代码,不过……

敲黑板,画重点,注意听了啊,只讲一遍,目前只有名称和描述可以直接改,其他的要看到修改后的结果只能先对App执行清除数据操作,那么问题来了,渠道还有哪些属性呢,我们不防点进去看看。

image

我们结合代码来看下。

// 设置提示音,IMPORTANCE_DEFAULT及以上才会有声音
channel.setSound(Uri.parse("..//aa.mp3"), AudioAttributes.Builder().build())
// 震动设置
channel.enableVibration(true)
// 设置震动模式,不设置使用系统默认
channel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
// 闪烁指示灯设置
channel.enableLights(true)
// 指示灯颜色设置(不是每一个手机都支持哦)
channel.lightColor = Color.RED
// 屏幕锁定时通知显示方式(无法更改)
channel.lockscreenVisibility = Notification.VISIBILITY_SECRET
// 覆盖勿扰设置(无法更改)
channel.setBypassDnd(true)
// 获取NotificationManager
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// 通知描述
channel.description = "测试通知描述"
// 创建通知渠道
notificationManager.createNotificationChannel(channel)

其中“lockscreenVisibility”和“setBypassDnd”是无法生效的,因为从源码中来看,

只能被系统或排序服务(Android Notification Ranking Service)更改。


image image

那么通知怎么跟通知渠道关联呢?Api26以上,创建通知需要提供渠道ID。

// 根据渠道创建通知
val notificationBuilder = Notification.Builder(this@TestActivity,
        "渠道ID")
        .setSmallIcon(R.drawable.ic_notification_logo)
        .setContentTitle("测试通知标题")
        .setContentText("测试通知内容")

// 弹出通知
notificationManager.notify(1, notificationBuilder.build())

接下来我们看看如果给渠道分组。渠道组对象是NotificationChannelGroup,也有一个ID和一个名称,创建一个渠道组很简单:

// 创建一个渠道组
val channelGroup = NotificationChannelGroup("测试组ID", "渠道组名")
// 绑定渠道组
channel.group = "测试组ID"
notificationManager.createNotificationChannelGroup(channelGroup)

上面代码创建渠道组和完成渠道与渠道组的绑定,如果渠道有绑定渠道组,必须先创建渠道组,再创建渠道哦。
关于渠道和渠道组的查询删除,就不用多说了吧。

// 查询所有当前用户的所有渠道
notificationManager.notificationChannelGroups
// 查询所有当前用户的所有渠道组
notificationManager.notificationChannels
// 根据ID删除渠道
notificationManager.deleteNotificationChannel("渠道ID")
// 根据ID删除渠道组
notificationManager.deleteNotificationChannel("测试组ID")

下面几个特性倒简单,我就稍微说说了。

图片偷自于网上

通知角标

image

通知圆点可以在创建渠道时指定,要是创建时没指定修改时指定记得给App清除数据哦

// 显示通知圆点
channel.setShowBadge(true)

打盹儿

image

自动消失

val notificationBuilder = Notification.Builder(this@TestActivity,
        .setTimeoutAfter(5000L)

都很正常,然而坑往往就。。。

▲图片偷自于网上

setSettingText

image

大概意思还是能猜到的,就是说8.0你可以通过INTENT_CATEGORY_NOTIFICATION_PREFERENCES 给通知加一个入口到你应用的通知设置里,而且还可以用setSettingsText()给入口指定文字。给个页面或demo会死啊,通知那一章节也只字不提这玩意儿了。再进setSettingText():

image

什么是affordance,难道不是下面这个齿轮嘛,难不成下面"MORE SETTINGS"这块文字能改?还是too young啊!

image

无奈只好谷歌呗。

image image

我竟然寄希望去百度了。

image image

看到第一条喜出望外啊!谁知道特么是官方文档的翻译,多的图就不截了,谁搜谁知道。可不能就这样放弃啊,今早从overflow上搜到了唯一一个,不过是自己昨晚提的。

image

试着从INTENT_CATEGORY_NOTIFICATION_PREFERENCES突破,总算有一点点进步。

<activity android:name=".AppNotificationSettingsActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>

        <category android:name="android.intent.category.
NOTIFICATION_PREFERENCES"/>
    </intent-filter>
</activity>
image

本以为setSettingText() 可以改变上面文字,又一次失望了,昨晚看了youtube上的几个片子,均无介绍。就连兼容类NotificationCompat.Builder中都无setSettingText()方法。好吧,我决定看了源码之后再告诉你们,放我一条生路先!

再来看第二坑

image

傻乎乎地敲完了,试着移除几个通知,毛线都没有啊。

class NLService : NotificationListenerService(){

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        super.onNotificationPosted(sbn)
        if(packageName == sbn?.packageName)
            Log.i(TAG, "onNotificationPosted" + sbn.toString())
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        super.onNotificationRemoved(sbn)
        if(packageName == sbn?.packageName)
            Log.i(TAG, "onNotificationRemoved" + sbn.toString())
    }
<service
    android:name=".NLService"
    android:label="@string/nlservice_name"
    android:permission="android.permission.BIND_NOTIFICATION_
LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.
NotificationListenerService"/>
    </intent-filter>
</service>
▲图片偷自于网上

原来是要手动开启通知访问权限啊。以Pixel为例,设置>应用和通知>高级>特殊应用权限->通知使用权。

还有坑?

▲图片偷自于网上 image

看样子是能改背景色?好吧。

image

这是背景色吗?这是背景色吗?

机智如我,看到这下面这句话。

image

于是代码就成这样喽。

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    val notification = intent?.getParcelableExtra<Notification>(ARG_NOTIFICATION)
    if(notification != null){
        startForeground(1, notification)
        mHandler?.postDelayed(Runnable {
            stopForeground(true)
        }, TestNotificationColorService.DURATION_NOTIFICATION_DISMISS)
    }
    return super.onStartCommand(intent, flags, startId)
}

终于硬了一把。

image

最后一个终于不是坑了,大家放心踩,出了问题找我。

image
// If Showing message style notification, create some test data.
val showMessageStyleView = view?.findViewById<CheckBox>(R.id.message_style_ck)
if(showMessageStyleView != null && showMessageStyleView.isChecked){
    builder.setStyle(NotificationCompat.MessagingStyle(getString(R.string.test_display_name))
            .setConversationTitle(getString(R.string.test_conversation_title))
            .addMessage(getString(R.string.test_message_chat1), System.currentTimeMillis(), getString(R.string.test_sender))
            .addMessage(getString(R.string.test_message_chat2), System.currentTimeMillis(), getString(R.string.test_sender)))
}

以上demo已上传至:https://github.com/andrsay/OreoNotificationSample

image
一个一年可能只更新一次的公众号!
上一篇下一篇

猜你喜欢

热点阅读