有沒有辦法使用NotificationManagerCompat和NotificationCompat時設置在Android奧利奧渠道?NotificationManagerCompat Android上的奧利奧
4
A
回答
7
由於NotificationManagerCompat只是一個包裝類,使生活更輕鬆,你可以正常創建渠道:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_title)
val description = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.parseColor("#5B3C88")
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
val manager = (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
manager.createNotificationChannel(mChannel)
}
然後當你發佈通知使用NotificationManagerCompat,但不要忘了構造通知使用新的構造:
NotificationCompat.Builder(context, CHANNEL_ID)
1
我通常使用這個類來管理的通知信道:
class NotificationManager(private val context: Context) {
companion object {
private val CHANNEL_ID = "YOUR_CHANNEL_ID"
private val CHANNEL_NAME = "Your human readable notification channel name"
private val CHANNEL_DESCRIPTION = "description"
}
@RequiresApi(Build.VERSION_CODES.O)
fun getMainNotificationId(): String {
return CHANNEL_ID
}
@RequiresApi(Build.VERSION_CODES.O)
fun createMainNotificationChannel() {
val id = CHANNEL_ID
val name = CHANNEL_NAME
val description = CHANNEL_DESCRIPTION
val importance = android.app.NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(id, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.RED
mChannel.enableVibration(true)
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
mNotificationManager.createNotificationChannel(mChannel)
}
}
然後你可以使用UTIL這樣
fun createNotificationCompatBuilder(context: Context): NotificationCompat.Builder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return NotificationCompat.Builder(context, NotificationManager(context).mainNotificationId)
} else {
return NotificationCompat.Builder(context)
}
}
這樣,您就可以與簽名應用程序的任何地方使用它,就像你以前使用過,你可以很容易地改變它在未來的變化情況。
相關問題
- 1. Android上的服務奧利奧
- 2. 奧地利奧地利和奧地利的地理地圖
- 3. Android奧利奧WideColorGamut/ProPhotoRGB不被識別
- 4. Settings.canDrawOverlays(上下文)Android上的奧利奧返回false
- 5. NotificationListenerService沒有綁定到Android 8上的SystemUI(奧利奧)
- 6. Xamarin奧利奧自適應圖標
- 7. 閱讀短信與奧利奧
- 8. 奧利奧 - 在前臺開始服務
- 9. 奧雷利奧獲取無限循環
- 10. Android的奧利奧背景位置限制
- 11. Android的奧利奧 - 推送通知崩潰
- 12. 福利奧輪廓
- 13. 奧利奧模擬器上沒有顯示通知
- 14. 奧裏利亞奧裏利亞-HTTP客戶端JSONP
- 15. 奧裏利亞repeat.for限制
- 16. .bind VS在奧裏利亞
- 17. 參考在奧裏利亞
- 18. 奧裏利亞repeat.for項目
- 19. 相關的更改對於奧利奧提供錯誤
- 20. 在Android O(奧利奧)中更改模式首選項的命令
- 21. 在奧利奧上小心翼翼地獲取iBeacon的主要,次要和UUID
- 22. 升級到奧利奧後應用程序中斷
- 23. 奧利奧:禁用活動轉換動畫
- 24. 安卓「O」(奧利奧,8)媒體按鈕問題
- 25. 崩潰在Android 8.0(API 26,奧利奧)Twilio聊天客戶端關機期間
- 26. 奧裏利亞編輯欄的keydown
- 27. PlayServices過期通知需要安卓奧利奧(API 26)的Notifcation頻道
- 28. 奧裏利亞JS加NPM包
- 29. 綁定selectize與奧裏利亞
- 30. 設置對話框奧裏利亞