12 AndroidNotification
12 AndroidNotification
12 AndroidNotification
Contents
● What are notifications?
● Notification channels
● Creating a notification channel
● Creating notifications
● Handling notification actions
What are notifications
● Notification: A message displayed to the user
outside of any app's UI in a top notification
drawer area.
– used to indicate system events, status
of service tasks, etc.
What are notifications
● Notifications can have:
○ icons (small, large)
○ a title
○ a detailed description
5
Notification channels
6
Notification channels are mandatory
● Notification channels are introduced in Android 8.0 (API level 26)
● All notifications must be assigned to a channel starting from Android
8.0 (API level 26), else your notifications will not be displayed.
● For the apps targeting lower than Android 8.0 (API level 26), no need
to implement notification channels.
7
Notification channels in Settings
● Notification channels appear as
Categories under App notifications in
the device Settings.
8
Create a Notification channel
● Notification channel instance is created using NotificationChannel constructor.
● You must specify:
○ An ID that's unique within your package.
○ User visible name of the channel.
○ The importance level for the channel.
10
Notification priority
● Determines how the system displays the notification with respect to
other notifications, in Android version Lower than API level 26.
● Set using the setPriority() method for each notification.
● Range from PRIORITY_MIN to PRIORITY_MAX.
setPriority(NotificationCompat.PRIORITY_HIGH)
11
Importance level and priority constants
Importance (Android 8.0 Priority (Android 7.1
User-visible importance level
and higher) and lower)
Urgent
PRIORITY_HIGH or
Makes a sound and appears as a heads-up IMPORTANCE_HIGH
PRIORITY_MAX
notification
High
IMPORTANCE_DEFAULT PRIORITY_DEFAULT
Makes a sound
Medium
IMPORTANCE_LOW PRIORITY_LOW
No sound
Low
IMPORTANCE_MIN PRIORITY_MIN
No sound and doesn't appear in the status bar
12
Creating notifications
● Notification is created using NotificationCompat.Builder class.
● Pass the application context and notification channel ID to the
constructor.
● The NotificationCompat.Builder constructor takes the
notification channel ID, this is only used by Android 8.0 (API level 26)
and higher, but this parameter is ignored by the older versions.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.android_icon)
.setContentTitle("You've been notified!")
.setContentText("This is your notification
text.");
Delivering notifications
manager.notify(ID, notification)
Updating notifications
1. Update a notification by changing and or adding some of its content.
2. Issue notification with updated parameters using builder.
3. Call notify() passing in the same notification ID.
● If previous notification is still visible, system updates.
● If previous notification has been dismissed, new notification is
delivered.
17
Canceling notifications
Notifications remain visible until:
● User dismisses it by swiping or by using "Clear All".
● Calling setAutoCancel() when creating the notification, removes
it from the status bar when the user clicks on it.
● App calls cancel() or cancelAll() on
NotificationManager.
mNotifyManager.cancel(NOTIFICATION_ID);
18
Handling notification actions
● Normally when the user clicks on a notification, an action should occur. (direct the user to a
particular app / activity, etc.)
– To achieve this, use an intent inside your notification.
– Must wrap it inside a "pending intent" object.
Notification.Builder builder = ...;
Intent intent = new Intent(this, ActivityClassName.class);
intent.putExtra("key1", "value1");
...
PendingIntent pending = PendingIntent.getActivity(
this, 0, intent, 0);
builder.setContentIntent(pending);
Notification notification = builder.build();
19
Classwork
● When opening the app, if there is product in the cart
○ Show a notification on status bar to remind user about selected products
○ When clicking on the notification, open a Cart screen
References
● https://google-developer-training.github.io/android-developer-fun
damentals-course-concepts-v2/