1 of 8

Notification

Prasanna Kumar K R

AP/IT

2 of 8

Notification

  • Notifications provide short, timely information about events in your app while it's not in use.

3 of 8

4 of 8

Set the notification content�

  • To get started, you need to set the notification's content and channel using a NotificationCompat.Builder object. The following example shows how to create a notification with the following:
  • A small icon, set by setSmallIcon(). This is the only user-visible content that's required.
  • A title, set by setContentTitle().
  • The body text, set by setContentText().
  • The notification priority, set by setPriority(). The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance—shown in the next section.)

5 of 8

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)�        .setSmallIcon(R.drawable.notification_icon)�        .setContentTitle(textTitle)�        .setContentText(textContent)�        .setPriority(NotificationCompat.PRIORITY_DEFAULT);�

6 of 8

Set the notification's tap action

  • Every notification should respond to a tap, usually to open an activity in your app that corresponds to the notification.

7 of 8

Intent intent = new Intent(this, AlertDetails.class);�intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);�PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);��NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)�        .setSmallIcon(R.drawable.notification_icon)�        .setContentTitle("My notification")�        .setContentText("Hello World!")�        .setPriority(NotificationCompat.PRIORITY_DEFAULT)�        // Set the intent that will fire when the user taps the notification�        .setContentIntent(pendingIntent)�        .setAutoCancel(true);�

8 of 8

Show the notification

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);��// notificationId is a unique int for each notification that you must define�notificationManager.notify(notificationId, builder.build());