DevCrew I/O
  • HOME
  • SERVICES
  • WORK
  • ABOUT US
  • BLOG
  • GET IN TOUCH
  • HOME
  • SERVICES
  • WORK
  • ABOUT US
  • BLOG
  • GET IN TOUCH

First Look at Android O Notifications

May 16, 2017  |  By Zohaib Akram In Android, Android Development, Blog, Programming
android_o_cover

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:

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.

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.

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.
Android o notification
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.

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.

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.

android-development android-notification android-O

Article by Zohaib Akram

Zohaib Akram is an Android developer with extensive experience in designing and developing Android apps. He likes to play racing and simulation games. He is always into learning and adapting new things related to technology.

Previous StoryUnity Editor Scripting (A kick-starter guide) – Part 2
Next StoryUnity Editor Scripting (A kick-starter guide) – Part 3

Related Articles

  • be89d865995a1611c45849f692310278c3fb0561
    Having an app idea but no clue of development process?
  • sensors1
    App Development Trends/ Sensors

Leave your comment Cancel Reply

(will not be shared)

Categories

  • Android (4)
  • Android Development (6)
  • App Development (2)
  • Blockchain (1)
  • Blog (35)
  • Branding (1)
  • Game Development (5)
  • Identity (2)
  • In the press (1)
  • Machine Learning (5)
  • Productivity (6)
  • Programming (14)
  • Project Management (5)
  • Uncategorized (4)
  • Unity (5)
  • VR (2)
  • Web Development (2)

Tags

agile android android-development android-notification android-O Artificial Intelligence Deep Learning DEVELOPMENT easter eCommerce editor editor scripting editor window entertainment game development GAMING holidays ios iphone jcenter kanban Machine Learning macos Magento maven mobile motivation movies oculus productivity programmer PROGRAMMING project management scaleform scriptable objects scripting scrum self-help Shopify siri time management UI unity unity. c# vr
DevCrew I/O
  • HOME
  • SERVICES
  • WORK
  • ABOUT US
  • BLOG
  • GET IN TOUCH

Copyright ©2017 DevCrew.io. All Rights Reserved.