Android has recently released the first preview of Android O in which they have added some new features. Today we will talk about what are new features added in Notifications.
Notification Channel
Notification channel allow the developer to create custom channel for their notification. User can block the notifications of specific categories (channels). To create the notification channel SDK has new class NotificationChannel that need the id of channel (it should be unique) the name and the importance of channel. So first you need to download the new Android O API and set your app build file with following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
android { compileSdkVersion 'android-O' buildToolsVersion '26.0.0-rc1' defaultConfig { applicationId "com.example.notificationexample" minSdkVersion 26 targetSdkVersion 'O' versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } |
You can learn more about how to migrate your app to Android O.
1 2 3 4 5 6 |
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String id = "my_channel_01"; CharSequence name = "Peter"; int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(id, name, importance); mNotificationManager.createNotificationChannel(mChannel); |
The above code create the channel with name ‘Peter’ and unique id “my_channel_01”. In my example all messages related to Peter will show in Peter channel. Let create message.
1 2 3 4 5 6 7 |
int notifyID = 1; Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages from Peter") .setSmallIcon(R.mipmap.ic_launcher) .build(); mNotificationManager.notify(notifyID, notification); |
The code above will send the notification same as we used to do before.
Hold and press the notification it will show the detail of notification like below.
Click on ALL CATEGORIES. It will takes you to Notification setting of your app and you can see all channels of notifications there and you can manage them from there also.
In the screenshot above you can see the Peter channel that we recently created above.
Notification Color
You can change the color of notification now in Android. It is important when you want to notify your user that some thing need urgent to be done etc. Just set true setColorized(true) and set color by using setColor(color) in notification builder.
1 2 3 4 5 6 7 |
Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages from Peter") .setSmallIcon(R.mipmap.ic_launcher) .setColorized(true) // this will enable to draw color .setColor(Color.RED) // this will set your color .build(); |
You can see the red color of notification title and to highlight the user.
Notification TimeOut
User can set the time to cancelled their notification after specific time period. Just add the setTimeout(long) in notification builder.
1 2 3 4 5 6 7 8 |
Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages from Peter") .setSmallIcon(R.mipmap.ic_launcher) .setColorized(true) // this will enable to draw color .setColor(Color.RED) // this will set your color .setTimeout(10000) // this notification will be automatically cancelled after 10 secs. .build(); |
You can learn more about the notification features from Android Developer Site.