Android O 適配通知欄,發(fā)送通知不生效,報(bào)錯(cuò)如下:
E/NotificationService: No Channel found for pkg=***, channelId=my_channel_01, id=0, tag=null, opPkg=***, callingUid=10219, userId=0, incomingUserId=0, notificationUid=10219, notification=Notification(channel=my_channel_01 pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)
重要特性:NotificationChannel
// 最終代碼:
val notificationManager = (context as Context).getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
val notificationId = 0x1234
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
var mChannel = NotificationChannel("1", "my_channel_01" as CharSequence, NotificationManager.IMPORTANCE_DEFAULT)
mChannel.enableLights(true)
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
notificationManager!!.createNotificationChannel(mChannel)
var builder = Notification.Builder(context, "1")
builder.setSmallIcon(android.R.drawable.stat_notify_chat)
.setContentTitle("開心不開心")
.setContentText("開心")
.setNumber(3); //久按桌面圖標(biāo)時(shí)允許的此條通知的數(shù)量
notificationManager!!.notify(notificationId, builder.build());
} else {
TODO("VERSION.SDK_INT < O")
}
其他
// 點(diǎn)擊后自動(dòng)消失
.setAutoCancel(true)
Kotlin 也是有數(shù)組的,不要被誤導(dǎo)了
var intents:Array<Intent> = arrayOf(Intent(context, MainActivity::class.java),Intent(context, NoticeActivity::class.java))
// 設(shè)置多個(gè)intent,開啟多個(gè)Activity,此處不能用集合,只能用數(shù)組
.setContentIntent(PendingIntent.getActivities(context, 1, intents, PendingIntent.FLAG_CANCEL_CURRENT))
參考鏈接
https://blog.csdn.net/rentee/article/details/78303532
強(qiáng)烈推薦
我是在解決之后,無意看到郭神的 《Android 8.0 通知欄適配》
大神之作,受益匪淺!