Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 3, 2017 02:00 pm

Android O: How to Use Notification Channels

Final product image
What You'll Be Creating

Google has launched the first developer preview of the next Android version, currently code-named Android O (Oreo maybe?). Some exciting features were released, and one of them is Notification Channels. In this tutorial, we'll explore this feature and build a simple app that demonstrates the functionalities it provides. 

What Are Notification Channels?

Notification channels enable us app developers to group our notifications into groups—channels—with the user having the ability to modify notification settings for the entire channel at once. For example, for each channel, users can completely block all notifications, override the importance level, or allow a notification badge to be shown. This new feature helps in greatly improving the user experience of an app. 

We're going to learn about this feature by building a simple application called "TutsplusAlerts" that will provide two notification channels: Android and iOS. The user will receive a notification from one of these separate channels whenever a new article is submitted.


1. Set Up the Android O SDK

To begin to use Android O APIs as of this writing, you will need to have the latest Android Studio 2.4 Canary installed on your computer. 

Launch Android Studio 2.4 and open the SDK Manager by clicking Tools > Android > SDK Manager.

Then, in the SDK Platforms tab, check Show Package Details. Below Android O Preview, check the following: Android SDK Platform O and Google APIs Intel x86 Atom System Image (required only for the emulator).

Android Studio SDK Manager

Then switch to the SDK Tools tab, and select the following: 



  • Android SDK Build-Tools 26.0.0 (rc1 or higher)


  • Android SDK Platform-Tools 26.0.0 (rc1 or higher)

  • Android Emulator 26.0.0

  • Support Repository

Click the OK button to download all of these components.


2. Create an Android Studio Project

In your Android Studio, create a new project named TutsplusAlerts with an empty activity called MainActivity.

Android Studio new project setup


3. Update build.gradle

Go to your app module build.gradle file and update compileSdkVersion, buildToolsVersion and targetSdkVersion and finally the Support Library version.

Remember to sync your project after making these changes.


4. Create Notification Channels

Create a new class and name it NotificationUtils, extending ContextWrapper.

In the code above, we created two instances of the NotificationChannel, passing an id (which must be unique within your package), a channel name, and also an importance level in its constructor. For each notification channel, we applied characteristics such as sound, lights, vibration, and a notification to show on the lock screen. Finally, we got the NotificationManager from the system and then registered the channel by calling the method createNotificationChannel(), passing the channel we have created.

We can create multiple notification channels all at once with createNotificationChannels(), passing a Java list of NotificationChannel instances. You can get all notification channels for an app with getNotificationChannels() and get a specific channel with getNotificationChannel(), passing only the channel id as an argument.

Importance Levels

Starting from Android O, priority levels are deprecated for individual notifications. Instead, you set an importance level when creating a notification channel—ranging from NotificationManager.IMPORTANCE_NONE to NotificationManager.IMPORTANCE_HIGH.  We'll set the Android channel to be IMPORTANCE_DEFAULT, while that of the iOS channel will be IMPORTANCE_HIGH

The full list of importance options available are:



  • IMPORTANCE_MAX: unused


  • IMPORTANCE_HIGH: shows everywhere, makes noise and peeks



  • IMPORTANCE_DEFAULT: shows everywhere, makes noise, but does not visually intrude



  • IMPORTANCE_LOW: shows everywhere, but is not intrusive


  • IMPORTANCE_MIN: only shows in the shade, below the fold


  • IMPORTANCE_NONE: a notification with no importance; does not show in the shade

All notifications for a channel will be given the same importance level.


5. Create Notifications and Post to Channels

We are going to create two Notifications for each of our channels in the NotificationUtils class we created. We specify which notification should be sent to a channel in the Notification.Builder (Android API 25) constructor, where we pass it the channel id as the second argument. 

Be aware that Notification.Builder() also has a notification channel id setter method called setChannel(String channelId), so you can choose to set the notification channel id either in the constructor or using the setter method.


6. Create the XML Layout

Now that we have the setup for creating and posting to notification channels, let's create the XML layout interface for posting the message in our activity_main.xml


7. Post Notifications to Channels

Posting to Android Channel

In this section, we are going to edit our MainActivity so that we can get the title and author from the EditText components and then send these to the Android channel. We get the Notification.Builder for the Android channel we created in our NotificationUtils, and then notify the NotificationManager.

At this point, run the app and enter a title and an author, and then click the send button to receive the notification immediately.

Android app emulator send notification shows in notification drawer

Posting to the iOS Channel

Here we would post to the iOS channel. We get the Notification.Builder for the iOS channel we created in our NotificationUtils and then call the notify() method in the NotificationManager.

Run the app again and enter a title and an author, and then click the send button to receive the notification immediately.

Android app emulator send button click shows notification in drawer


8. Notification Channel Settings

As of this writing, you can't programmatically change the configuration of specific notification channel settings. Instead, the only option available is for the user to go to the notification settings screen for the app in the device settings. From there, the user can access the app notification settings to modify settings such as vibration, sound, etc. Users can navigate to the app notification settings in either of the following ways:


  • Long pressing on the notification on the notification drawer (left image below).


  • Settings > Apps & notifications > Notifications > then selecting the app (right image below). 

Android Emulator app notification settings

You can also send the user right from your app to the channel notification settings. Let's see how we can do that for the Android channel. It's recommended that you do this in your app settings to make it easy for users to access these notification options.

Edit the XML Layout

Include another button that will send the user to the channel notification settings.

Code the Intent

Here we create an intent and pass it a Settings action—ACTION_CHANNEL_NOTIFICATION_SETTINGS (API 25)—and then add some extra values: the app package name and channel id. Finally, we start the settings activity with the intent. 

Run the app and click the notification settings for the Android channel.

Android emulator click android channel notification settings opens device channel settings

In the channel notification settings, users can edit settings such as enabling vibrations, changing the importance, or showing a badge (if supported) for the channel. 

If you want to take users to the general notification settings for your app, you can also do that with an Intent:


9. Create Notification Groups

We can also group notification channels into groups so they can be managed together. This is useful for apps that support multiple user accounts. The same notification channels are available across the individual accounts. For example, a social networking app might include support for both personal and business user accounts. The code below shows you how to create a notification channel group:

We used the NotificationManager createNotificationChannelGroup() method, passing it an instance of NotificationChannelGroup, which needs the group id and group name to create the instance. 

Once that is done, we need to connect a notification channel to a group by using the NotificationChannel.setGroup() method and passing it the group id. If you want to create multiple notification groups all at once, use createNotificationChannelGroups(), passing it a Java list of NotificationChannelGroup instances.


10. Delete a Notification Channel

Deleting a notification channel is easy if it is no longer needed. Just use the notification manager method deleteNotificationChannel() and pass the channel id. 

However, be aware that deleted channels remain visible in the notification settings so as to prevent spam. 

Conclusion

In this tutorial, you learned about Notification Channels for Android O: what they are and how to create one, as well as how to post a notification to a channel, how to access a channel's notification settings, how to group notification channels, and how to delete a notification channel. 

To learn more about Notification Channels, do refer to the official documentation. And in the meantime, check out some of our other courses and tutorials on Android app development!



Original Link:

Share this article:    Share on Facebook
No Article Link

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code