Show Notification in Android

 A notification is a message that appears outside an application's UI to get user attention, provide reminders, notify about events, etc. A typical notification consists of a small icon, title, and content text.

Android 8.0 (API level 26) and higher versions of the platform, requires that notification should be assigned to a channel, otherwise it will not appear. A channel allows grouping notifications. User can manage settings of specific notification channel instead of managing all notifications individually.

First we need to construct a NotificationChannel object with a unique channel ID, a name, and an importance level. After that, a notification channel should be created using the method createNotificationChannel(). This method can be called multiple times with the same channel ID used previously, the channel will not be created again.

A notification can be build using Notification.Builder. The method notify() can be used to display a notification. This method requires the notification ID. It can be used later if you want to update or remove the notification.


package com.example.camera3;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class notification extends AppCompatActivity {

private String channelId = "my_channel";
private NotificationManager notifManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);

showNotification(1);
       showNotification(2);

}

private void showNotification(int id) {
    String channelId = "my_channel";
final int NOTIFY_ME_ID=id;
/*********** Create notification channel ***********/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "MyChannel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
/*********** Create notification ***********/
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Android Example Notification Title")
.setContentText("This is the android example notification message")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);

// This pending intent will open after notification click
Intent intent = new Intent(this,StudentNotificationActivity.class);
PendingIntent i = PendingIntent.getActivity(this, 0, new Intent(this, StudentNotificationActivity.class), PendingIntent.FLAG_IMMUTABLE);

builder.setContentIntent(i);

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(NOTIFY_ME_ID, builder.build());
}
    private void showMultipleNotifications(ArrayList<StudentNotification> notificationArrayList) {
            // one notification in multiple show        
         int notificationId = 1;
final int NOTIFY_ME_ID = 1337;

/*********** Create notification channel ***********/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "MyChannel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

Bitmap bitmap = null;
/*********** Create notifications ***********/
for (StudentNotification student : notificationArrayList) {
String imageURL = Api.BASE_URL + "api/get-user-image/UserImages/" + "Student" + "/" + student.getImage();
//
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(student.getCourseName())
.setContentText(student.getName() + "\n" + student.getDate())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);

// This pending intent will open after notification click
Intent intent = new Intent(this, StudentNotificationActivity.class);
PendingIntent i = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(i);

NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId++, builder.build());
}
}

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.