banner



What Is Data Download Notification Channel

Starting in Android viii.0 (API level 26), all notifications must be assigned to a channel. For each channel, you lot can set up the visual and auditory behavior that is practical to all notifications in that channel. Then, users tin can alter these settings and decide which notification channels from your app should exist intrusive or visible at all.

Check out the video below for an overview of channels and other new notification features in Android 8.0.

For sample code that uses notifications, including Android 8.0 features, meet the Android Notifications Sample.

The user settings for notification channels are available for each app in the system settings, every bit shown in figure 1.

Figure ane. Notification settings for the Clock app and one of its channels

Afterwards you lot create a notification aqueduct, yous cannot change the notification behaviors—the user has complete control at that indicate. Though you tin still alter a channel'due south proper noun and description.

You should create a channel for each distinct blazon of notification you need to send. Yous tin can as well create notification channels to reflect choices made by users of your app. For case, you can fix up separate notification channels for each conversation group created by a user in a messaging app.

When you lot target Android 8.0 (API level 26), yous must implement i or more notification channels. If your targetSdkVersion is fix to 25 or lower, when your app runs on Android eight.0 (API level 26) or college, it behaves the same equally it would on devices running Android vii.1 (API level 25) or lower.

Create a notification channel

To create a notification aqueduct, follow these steps:

  1. Construct a NotificationChannel object with a unique aqueduct ID, a user-visible name, and an importance level.
  2. Optionally, specify the description that the user sees in the organisation settings with setDescription().
  3. Annals the notification channel by passing information technology to createNotificationChannel().

Kotlin

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {     // Create the NotificationChannel     val proper name = getString(R.string.channel_name)     val descriptionText = getString(R.cord.channel_description)     val importance = NotificationManager.IMPORTANCE_DEFAULT     val mChannel = NotificationChannel(CHANNEL_ID, proper name, importance)     mChannel.description = descriptionText     // Register the aqueduct with the organization; you tin can't change the importance     // or other notification behaviors after this     val notificationManager = getSystemService(NOTIFICATION_SERVICE) equally NotificationManager     notificationManager.createNotificationChannel(mChannel) }            

Java

private void createNotificationChannel() {     // Create the NotificationChannel, but only on API 26+ because     // the NotificationChannel class is new and non in the support library     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {         CharSequence name = getString(R.cord.channel_name);         String description = getString(R.string.channel_description);         int importance = NotificationManager.IMPORTANCE_DEFAULT;         NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);         aqueduct.setDescription(description);         // Register the aqueduct with the system; you can't alter the importance         // or other notification behaviors after this         NotificationManager notificationManager = getSystemService(NotificationManager.course);         notificationManager.createNotificationChannel(channel);     } }            

Creating an existing notification aqueduct with its original values performs no performance, so it's safe to call this code when starting an app.

By default, all notifications posted to this channel utilize the visual and auditory behaviors defined past the importance level from the NotificationManagerCompat course, such as IMPORTANCE_DEFAULT and IMPORTANCE_HIGH. (See below for more data about importance levels.)

If you'd like to further customize your aqueduct's default notification behaviors, you tin can telephone call methods such every bit enableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel. Just remember that once you create the aqueduct, you lot cannot alter these settings and the user has terminal control of whether these behaviors are active.

You tin can besides create multiple notification channels in a single performance past calling createNotificationChannels().

Prepare the importance level

Channel importance affects the interruption level of all notifications posted in the channel, and yous must specify information technology in the NotificationChannel constructor. You tin can utilize one of five importance levels, ranging from IMPORTANCE_NONE(0) to IMPORTANCE_HIGH(4). The importance level you assign to a aqueduct applies to all notification messages that you mail service to it.

To support devices running Android 7.i (API level 25) or lower, you must besides call setPriority() for each notification, using a priority abiding from the NotificationCompat form.

The importance (NotificationManager.IMPORTANCE_*) and priority constants (NotificationCompat.PRIORITY_*) map to the user-visible importance options every bit indicated in tabular array i.

Table 1. Channel importance levels

User-visible importance level Importance (Android 8.0 and higher) Priority (Android vii.1 and lower)
Urgent
Makes a audio and appears as a heads-up notification
IMPORTANCE_HIGH PRIORITY_HIGH or PRIORITY_MAX
High
Makes a sound
IMPORTANCE_DEFAULT PRIORITY_DEFAULT
Medium
No sound
IMPORTANCE_LOW PRIORITY_LOW
Low
No audio and does non appear in the status bar
IMPORTANCE_MIN PRIORITY_MIN

All notifications, regardless of importance, announced in non-interruptive system UI locations, such as in the notification drawer and as a badge on the launcher icon, though you lot can modify the appearance of the notification badge.

Once you submit the channel to the NotificationManager, you cannot change the importance level. Nonetheless, the user can alter their preferences for your app's channels at any time.

For information most choosing an advisable priority level, see "Priority levels" in the Notifications design guide.

Read notification channel settings

Users can modify the settings for notification channels, including behaviors such every bit vibration and alert sound. So if yous'd like to know the settings a user has applied to your notification channels, follow these steps:

  1. Get the NotificationChannel object past calling either getNotificationChannel() or getNotificationChannels().
  2. Query specific channel settings such every bit getVibrationPattern(), getSound(), and getImportance().

Then, if y'all detect a channel setting that you believe inhibits the intended behavior for your app, you tin can propose the user change it and provide an action to open the channel settings (see the adjacent section).

Open the notification aqueduct settings

After you create a notification channel, you cannot alter the notification aqueduct's visual and auditory behaviors programmatically—only the user tin can modify the aqueduct behaviors from the system settings. To provide your users easy access to these notification settings, you lot should add an item in your app's settings UI that opens these arrangement settings.

You can open the arrangement settings for notification channels with an Intent that uses the ACTION_CHANNEL_NOTIFICATION_SETTINGS action.

For case, the following sample code shows how you tin redirect a user to the settings for a notification channel:

Kotlin

val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).utilize {     putExtra(Settings.EXTRA_APP_PACKAGE, packageName)     putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId()) } startActivity(intent)            

Coffee

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName()); intent.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId()); startActivity(intent);            

Find that the intent requires ii extras that specify your app'due south package proper name (also known equally the application ID) and the aqueduct to edit.

Delete a notification aqueduct

You can delete notification channels by calling deleteNotificationChannel(). The post-obit sample code demonstrates how to consummate this procedure:

Kotlin

// The id of the channel. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val id: String = "my_channel_01" notificationManager.deleteNotificationChannel(id)            

Java

NotificationManager notificationManager =         (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // The id of the aqueduct. Cord id = "my_channel_01"; notificationManager.deleteNotificationChannel(id);            

Create a notification channel grouping

If yous'd like to further organize the appearance of your channels in the settings UI, you can create channel groups. This is a good idea when your app supports multiple user accounts (such equally for work profiles), so you lot can create a notification channel group for each account. This way, users tin hands identify and command multiple notification channels that have identical names.

Figure two. Notification channel settings with groups for personal and piece of work accounts

For case, a social networking app might include support for personal and piece of work accounts. In this scenario, each business relationship might require multiple notification channels with identical functions and names, such as the post-obit:

  • A personal account with two channels:
    • New comments
    • Mail recommendations
  • A business business relationship with two channels:
    • New comments
    • Post recommendations

Organizing the notification channels into groups for each account ensures that users can easily distinguish between them.

Each notification channel grouping requires an ID that must be unique within your package, likewise as a user-visible proper noun. The post-obit snippet demonstrates how to create a notification aqueduct grouping.

Kotlin

// The id of the group. val groupId = "my_group_01" // The user-visible proper noun of the group. val groupName = getString(R.string.group_name) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager              notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupId, groupName))            

Coffee

// The id of the grouping. String groupId = "my_group_01"; // The user-visible name of the group. CharSequence groupName = getString(R.string.group_name); NotificationManager notificationManager =         (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);              mNotificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));            

After you lot've created a new group, y'all tin call setGroup() to acquaintance a new NotificationChannel object with the group.

Once yous submit the aqueduct to the notification manager, you lot cannot change the association between notification channel and grouping.

Source: https://developer.android.com/training/notify-user/channels

0 Response to "What Is Data Download Notification Channel"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel