Pro Android with Kotlin


Figure 8-4. Notification group Caution


Download 5.42 Mb.
Pdf ko'rish
bet134/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   130   131   132   133   134   135   136   137   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

Figure 8-4. Notification group
Caution Because of a bug at least in API level 27 you must add a summary notification for the 
grouping to be enabled at all. So, the advice is, no matter what API level you are targeting, add a 
notification summary.


153
CHAPTER 8: APIs
Notification Channels
Starting with Android 8.0 (API level 26), another way of grouping notifications by notification 
channel has been introduced. The idea is to give the device user more control over how 
notifications get categorized and prioritized by the system, as well as the way notifications 
get presented to the user.
To create a notification channel, you write the following, which we have already seen in the 
preceding sections:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel, but only
// on API 26+ only after that it is needed
val channel = if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.O) {
NotificationChannel(channelId,
"Channel Name",
NotificationManager.IMPORTANCE_DEFAULT)
} else {
throw RuntimeException("Internal error")
}
channel.description = "Description"
// Register the channel with the system
val notificationManager =
if (Build.VERSION.SDK_INT >=
Build.VERSION_CODES.M) {
getSystemService(
NotificationManager::class.java)
} else {
throw RuntimeException("Internal error")
}
notificationManager.
createNotificationChannel(channel)
}
Speaking of Kotlin language styling, this looks a little clumsy. The superfluous if constructs 
were introduced so Android Studio wouldn’t complain about compatibility issues. Adapt the 
channel ID, the channel name, and the importance in the channel constructor according to 
your needs, just as the description text.
By the way, the createNotificationChannel() method in the last line is idempotent. If a 
channel with the same characteristics already exists, nothing will happen.
The possible importance levels in the NotificationChannel constructor are IMPORTANCE_HIGH 
for sound and heads-up notification, IMPORTANCE_DEFAULT for sound, IMPORTANCE_LOW for no 
sound, and IMPORTANCE_MIN for neither sound nor status bar presence.
Having said that, it is up to the user how notification channels get handled. In your code 
you can still read the settings a user has made by using one of the get*() methods of the 
NotificationChannel object you can get from the manager via getNotificationChannel() or 
getNotificationChannels(). Please consult the online API documentation for details.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   130   131   132   133   134   135   136   137   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling