Simplest Way to Implement Notification Channel for Android 8.0 and above Download

Starting with Android 8.0 and above, all notifications MUST be assigned notification channelFor each notification channel, you can set the range of behavior that will be applied to all notifications in this channel, then the user can change these behaviors and decide which notification channels to pay attention to.

The notification channel code is available on Android Studio with API level 26 build tools. Basically, if you are using an older version, please update android studio and make sure the build tools are higher or equal API level 26,

How to create a notification channel Android 8.0

Notification channel will be ignored for all devices running Android 7 or lower.

To create a notification channel,

  1. you will need NotificaitonChannel object with a unique identifier, the name of the channel and the level of importance.
  2. If you wish, you can also create a channel description that will be displayed to users in the notification settings.
  3. Finally, you register this notification channel on NotificationManager object via createNotificationChannel ()

Creating an Android Project

So now let’s create a new Android application project from File> NewFill in all the necessary details.

In your content_main.xml file or layout file MainActivity, add the following code. We have just created a centered button that will create notifications on the click.

Create a class named NotificaitonHelper.java and paste the following code. I’m going to explain everything after the code, you should read these codes carefully so that you can understand them.

the hack.patchversion.patchversiontutorials package;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

public class NotificationHelper extends ContextWrapper {

private static final String CHANNEL_NAME = "Basic Notifications";
private static final String CHANNEL_DESCRIPTION = "Description of the notification";
private static end string CHANNEL_ID = "MyNotificationChannel";
private static NotificationManagertificationManager;
Private context database;

public NotificationHelper (Context database) {
super (base);

this.base = base;

if (Build.VERSION.SDK_INT> = Build.VERSION_CODES.O) {
CreateChannel ();
}
}

@RequiresApi (api = Build.VERSION_CODES.O)
private void createChannel () {
NotificationChanneltificationChannel = New NotificationChannel (CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription (CHANNEL_DESCRIPTION);
notificationChannel.enableVibration (true);
notificationChannel.enableLights (true);
notificationChannel.canShowBadge ();
notificationChannel.setLockscreenVisibility (Notification.VISIBILITY_PUBLIC);
notificationChannel.setLightColor (. GetResources () GetColor (R.color.colorAccent));

. GetNotificationManager () createNotificationChannel (notificationChannel);
}


public NotificationManager getNotificationManager () {
if (messagesManager == null) {
messagesManager = (NotificationManager) base.getSystemService (Context.NOTIFICATION_SERVICE);
}

Return notifications Manager;
}


// now allows you to create notifications

public void notify (String message, String title) {
NotificationCompat.Builder builder = new NotificationCompat.Builder (base, CHANNEL_ID);
UritificationUri = RingtoneManager.getDefaultUri (RingtoneManager.TYPE_NOTIFICATION);

builder.setContentTitle (title);
builder.setSmallIcon (R.drawable.ic_launcher_background);
builder.setContentText (post);
builder.setSound (notificationUri);
builder.setStyle (new NotificationCompat.BigTextStyle (). bigText (message));


getNotificationManager (). notify (9001, builder.build ());
}


}

As you can see from the code above, we created the recommended constructor in our NotificaitonHelper class. It is in this constructor that we create our notification channel. Remember that constructors are called as soon as an object has been created. Inside the constructor, please check if the Android version is greater than or equal to 8, if this is the case, create a notification channel, otherwise ignore, Android will take care of everything else. As we said earlier, the notification channel must be registered in the notification manager object, as shown in the code above. If you need to explain something, please ask in the comments system below, I will answer within a few minutes or even earlier.

Then add the following code to your MainActivity.java file.

the hack.patchversion.patchversiontutorials package;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate (BundlevedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
Toolbar toolbar = findViewById (R.id.toolbar);
setSupportActionBar (toolbar);

final NotificationHelpertificationHelper = new NotificationHelper (this);

Button notifyBtn = findViewById (R.id.notify_btn);

notifyBtn.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View View) {
messagesHelper.notify (“Notification”, “Header”);
}
});



}


}

VIDEO

Download: Link-1 |Link-2 |Link-3

Download

Leave a Comment

Your email address will not be published. Required fields are marked *