Some time ago, I took a look at some new features in Android 8 and shared them in my group. Prior to Android 8, notifications could not be categorized, so users could not categorize notifications that they were interested in or not interested in, which led to users suffering from recommendations and promotions when using certain apps. In order to further optimize the management of notifications, Google has modified and optimized notifications with the release of Android 8 to include notification channels.

Notification Channels

Here we try to give a simple definition of notification channels. Each notification belongs to a notification channel, and developers can create multiple notification channels freely in the APP. It should be noted that notification channels cannot be modified once created.

Create notification channels

Steps to create a Notification Channel in your application:

1. Create a NotificationChannel object using the constructor NotificationChannel(channelId, channelName, Importance)

2. Through createNotificationChannel () to register the NotificationChannel an object

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String channelId = "some_channel_id";
CharSequence channelName = "Some Channel";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notificationManager.createNotificationChannel(notificationChannel);
Copy the code

Three parameters are required to create a notification channel

  • ChannelId The ID of the notification channel can be any string, which is unique globally
  • ChannelName Specifies the name of the notification channel. This name is visible to users and should be carefully planned by developers
  • The importance level of the notification channel, which has several levels, can be manually changed by the user

    Second, we can manipulate the notification channel by using some of the public methods that the notification channel provides us:

  • GetId () – Retrieves the ID of a given channel
  • Enablellights () – Indicates whether this notification channel should be illuminated if the device in use supports notification lights
  • SetLightColor () – If we determine that the channel supports notification lights, it is allowed to pass an int value that defines the color used by the notification lights
  • EnablementVisuration () – when displayed on the device, indicates whether notifications from this channel should vibrate
  • GetImportance () – Retrieves the importance value for a given notification channel
  • SetSound () – Provides a Uri to play sound when notifications are published to this channel
  • GetSound () – Retrieves the sound assigned to this notification
  • SetGroup () – Sets the group to which notifications are assigned
  • GetGroup () – Retrieves the group to which the notification is assigned
  • SetBypassDnd () — Set whether notifications should bypass do Not Disturb mode (interrupt_filter _ priority value)
  • CanBypassDnd () – Retrieves whether notifications can bypass the Do not Disturb mode
  • GetName () – Retrieves the user-visible name of the specified channel
  • SetLockScreenVisibility () – Sets whether notifications from this channel should be displayed on the lock screen
  • Getlockscreendisibility () – Retrieve whether notifications from this channel will appear on the lockscreen
  • GetAudioAttributes () – Retrieves the audio attributes of the sound that has been assigned to the corresponding notification channel
  • CanShowBadge () – Retrieve whether a notification from this channel can be displayed as a badge in the launcher application. Below we write a demo to create two notification channels, upgrade and private message.
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "upgrade";
            String channelName = "Upgrade";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);
            channelId = "compose";
            channelName = "Private"; importance = NotificationManager.IMPORTANCE_DEFAULT; createNotificationChannel(channelId, channelName, importance); }} / / create a notification channel @ RequiresApi (API = Build. VERSION_CODES. O) private void createNotificationChannel (String channelId, String channelName, int importance) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); } public void sendUpgradeMsg(View view) { NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this,"upgrade")
                .setContentTitle("Upgrade")
                .setContentText("Programmers are finally off duty...")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .build();
        manager.notify(100, notification);
    }

    public void sendComposeMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "compose")
                .setContentTitle("Private")
                .setContentText("Someone sends you a private message with a question.") .setWhen(System.currentTimeMillis()) .setSmallIcon(R.drawable.icon) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)) .build(); manager.notify(101, notification); }}Copy the code

Manage notification channels

As we said earlier, once a notification channel is created, control is in the hands of the user, and if an important notification channel is manually closed by the user, we need to remind the user to manually open it.

The getNotificationChannel() method gets the specified notification channel object,

GetNotificationChannels () gets a collection of all notification objects, stored in a list

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = manager.getNotificationChannel("upgrade");
            if (channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
                Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel.getId());
                startActivity(intent);
                Toast.makeText(this, "Upgrade notifications cannot be turned off. Please turn notifications on manually.", Toast.LENGTH_SHORT).show();
            }
Copy the code

The simple use of this notification channel is introduced and referred to

Shoewann0402. Making. IO / 2018/01/08 /… Developer.android.com/about/versi…