Android 8.0 通知欄適配 & NotificationChannel

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 通知欄適配》
大神之作,受益匪淺!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容