info@androidpaper.co.in

How to display and manage Notifications in Android

To display and manage notifications in Android, you can follow these general steps:

Step 1: Create a Notification Channel (for Android 8.0 Oreo and above) Since Android 8.0 Oreo, you need to create a notification channel to group your notifications. This step ensures that your notifications comply with the updated notification behavior. Here's an example of creating a notification channel:

// Notification channel ID. You can create your own unique channel ID.
String channelId = "my_channel_id";

// Notification channel name and importance level
CharSequence channelName = "My Channel";
int importance = NotificationManager.IMPORTANCE_HIGH;

// Create the notification channel
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);

// Get the system notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Register the channel with the system
notificationManager.createNotificationChannel(notificationChannel);

Step 2: Build and display the notification To build and display a notification, you need to create a NotificationCompat.Builder object and set its properties. Here's an example:

// Create an intent to open your app when the notification is clicked
Intent intent = new Intent(this, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("message", "This is a notification message");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);


// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Notification Title")
        .setContentText("Notification Text")
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true); // Remove the notification when clicked

// Display the notification
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId, builder.build());

In the above code, replace YourActivity with the activity you want to open when the notification is clicked. Also, make sure to replace R.drawable.notification_icon with the resource ID of your own notification icon. Step 3: Handle notification clicks (optional) If you want to perform specific actions when the user clicks on the notification, you can handle it in YourActivity.class

   textView = findViewById(R.id.textView);
        //getting the notification message
        String message=getIntent().getStringExtra("message");
        textView.setText(message);

By following these steps, you can display notifications in your Android app using Java. Customize the notification's content, actions, and behavior according to your requirements.