Android Notification New features in Android 8.0+

With Android Notification in the Android app, you can give a message to the user like Reminders, alerts, Messages, etc even app not running or using. All most all Android apps sending a notification to customer or users. Like Flipkart, Amazon e-commerce apps sending about sales and other useful notifications. What’s app sending a notification about a new chat message no top of the android phone, you can tap the notification to open an app or take an action directly from the notification.

Android Notification New features in Android 8.0+ icon

Android Notification structure:

  • Small icon: Required and set with setSmallIcon().
  • App name: This is provided by the system.
  • Timestamp: Provided by the system but you can override with setWhen() or hide it with setShowWhen(false).
  • Title: optional and set with setContentTitle().
  • Text:  Its for details, optional and set with setContentText().
Android Notification New features in Android 8.0+ oreo p

It’s a basic detail of Android notification, it can vary with a version to version android OS.

Sourcehttps://developer.android.com/guide/topics/ui/notifiers/notifications

Notification Channels

From Android 8.0 (API level 26), all notifications must be assigned to a channel.

You can create multiple channels and for each channel, you can set diffract behavior and visual. The user can decide or set the setting which notification channels from the app should be visible at all.

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

 

After you create a notification channel, you cannot change the notification behaviors—the user has complete control at that point. But you can still change a channel’s name and description.

How Notifications show on Android devices :

  • Status bar and notification drawer
  • Lock Screen
  • Heads-up notification
  • App icon badge

Let’s Build a Simple app for Android Application 

Step 1. Create a new project “ Build Your First Android App in Kotlin “
Step 2. Create class NotificationHelper.java extends ContextWrapper
package in.eyehunt.androidnotification;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;

NotificationHelper extends ContextWrapper {
    private NotificationManager notifManager;
    public static final String CHANNEL_ONE_ID = "in.eyehunt.androidnotification.one";
    public static final String CHANNEL_ONE_NAME = "Channel One";

    //Create your notification channels
    public NotificationHelper(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {

        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                CHANNEL_ONE_NAME, notifManager.IMPORTANCE_HIGH);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setShowBadge(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(notificationChannel);

    }

    //Create the notification
    public Notification.Builder getNotification1(String title, String body) {
        return new Notification.Builder(getApplicationContext(), CHANNEL_ONE_ID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true);
    }

    public void notify(int id, Notification.Builder notification) {
        getManager().notify(id, notification.build());
    }

    //Send  notifications to the NotificationManager system
    private NotificationManager getManager() {
        if (notifManager == null) {
            notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return notifManager;
    }
}
Step 3. Add Strings in  a “strings.xml” resource file
<resources>
    <string name="app_name">Android Notification</string>
    <string name="text">Type message</string>
    <string name="button_label">Post Notification</string>
    <string name="settings">Settings</string>
    <string name="channel_one_body">Android tutorial new post - Eyehunt</string>
</resources>
Step 4. Open an “activity_main.xml” resource file

add button and following

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <EditText
        android:id="@+id/channel_one_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="@string/text"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:inputType="text"/>

    <Button
        android:id="@+id/post_to_channel_one"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/button_label" />

    <Button
        android:id="@+id/channel_one_settings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/settings" />

</LinearLayout>
Step 5. Add the following code in “MainActivity.xml”
package in.eyehunt.androidnotification;

import android.app.Notification;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private static final int notification_one = 1;
    private MainUi mainUI;

    private NotificationHelper notificationHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        notificationHelper = new NotificationHelper(this);
        mainUI = new MainUi(findViewById(R.id.activity_main));
    }

    //Postthe notifications
    public void postNotification(int id, String title) {
        Notification.Builder notificationBuilder = null;

        notificationBuilder = notificationHelper.getNotification1(title,
                getString(R.string.channel_one_body));

        if (notificationBuilder != null) {
            notificationHelper.notify(id, notificationBuilder);
        }
    }

    //Implement onClickListeners
    class MainUi implements View.OnClickListener {
        final EditText editTextOne;

        private MainUi(View root) {
            editTextOne = (EditText) root.findViewById(R.id.channel_one_text);
            ((Button) root.findViewById(R.id.post_to_channel_one)).setOnClickListener(this);
            ((Button) root.findViewById(R.id.channel_one_settings)).setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.post_to_channel_one:
                    postNotification(notification_one, editTextOne.getText().toString());
                    break;

                case R.id.channel_one_settings:
                    goToNotificationSettings(NotificationHelper.CHANNEL_ONE_ID);
                    break;

            }
        }

        //settings screen for the selected notification channel
        public void goToNotificationSettings(String channel) {
            Intent i = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
            i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
            i.putExtra(Settings.EXTRA_CHANNEL_ID, channel);
            startActivity(i);
        }
    }
}
Step 6. Run the application, in the emulator or On your android device

Output screenshot Android Notification example in Android :

Android Notification New features in Android 8.0+ oreo p output

Video Output

Download source code Android Android Notification example

https://github.com/EyeHunts/AndroidNotification

Note : This example (Project) is developed in Android Studio 3.1.3 . Tested on Android 9 ( Android-P), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”26″

TargetSdkVersion=”27″

Coding in Java

For open a notification you have to Add create Intent. Where Pending Intent waiting for trigger an action, Once a user clicks on the notification it will open particular comments lime Activity or any.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.