Part 1 – Notification Channels
Part 2 – Tap Action & Action Buttons
Part 3 – Large Icon, BigTextStyle & InboxStyle
Part 4 – BigPictureStyle & MediaStyle
Part 5 – MessagingStyle & Direct Reply
Part 6 – Progress Bar Notification
Part 7 – Notification Groups
Part 8 – NotificationChannelGroups
Part 9 – Notification Settings
Part 10 – Delete Notification Channels
Part 1 – Notification Channels
In part 1 we will start by creating notification channels (Categories) which are necessary since Android Oreo (API level 26) to be able to show any notifications. These channels should be created as soon as we start our app, so we will do it in the onCreate method of a class that extends Application.
When we create these channels, we have to pass an ID, a name and an importance level. Additionally we can customize the channel further, like enabling the LED, activating vibration or changing the sound, but the user has ultimate control over the channel’s behavior.
When our NotificationChannel objects are created, we pass them to the NotificationManager’s createNotificationChannel method.
When we then want to show a notification, we create it with the NotificationCompat.Builder, where we can set a title, a message, an icon, a priority level, a category and many more customization options, and then we pass this
Notification object to the NotificationManagerCompat’s notify method, where we also have to pass an ID which we can later use to update or cancel this particular notification.
In AndroidManifest.xml file add these lines of codes:-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deftminds.notificationsexample">
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In App.java class file add these lines of codes:-
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
public static final String CHANNEL_2_ID = "channel2";
@Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(
CHANNEL_1_ID,
"Channel 1",
NotificationManager.IMPORTANCE_HIGH
);
channel1.setDescription("This is Channel 1");
NotificationChannel channel2 = new NotificationChannel(
CHANNEL_2_ID,
"Channel 2",
NotificationManager.IMPORTANCE_LOW
);
channel2.setDescription("This is Channel 2");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
manager.createNotificationChannel(channel2);
}
}
}
In activity_main.xml file add these lines of codes:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" />
<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendOnChannel1"
android:text="Send on Channel 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="sendOnChannel2"
android:text="Send on Channel 2" />
</LinearLayout>
In MainActivity.java class file add these lines of codes:-
import android.app.Notification;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import static com.codinginflow.notificationsexample.App.CHANNEL_1_ID;
import static com.codinginflow.notificationsexample.App.CHANNEL_2_ID;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
private EditText editTextTitle;
private EditText editTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = NotificationManagerCompat.from(this);
editTextTitle = findViewById(R.id.edit_text_title);
editTextMessage = findViewById(R.id.edit_text_message);
}
public void sendOnChannel1(View v) {
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_one)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.build();
notificationManager.notify(1, notification);
}
public void sendOnChannel2(View v) {
String title = editTextTitle.getText().toString();
String message = editTextMessage.getText().toString();
Notification notification = new NotificationCompat.Builder(this, CHANNEL_2_ID)
.setSmallIcon(R.drawable.ic_two)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
notificationManager.notify(2, notification);
}
}
okay, now run on emulator to see the output!!