Alarms and Schedulers
Lesson 8
Android Developer Fundamentals V2
1
1
1
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
8.1 Notifications
2
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Contents
3
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What Are Notifications?
4
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What is a notification?
Message displayed to user outside regular app UI
5
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
How are notifications used?
6
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
App icon badge
Available only on the devices running Android 8.0 (API level 26) and higher.
7
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification
Channels
8
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification channels
9
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification channels are mandatory
10
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification channels in Settings
11
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Creating a Notification channel
12
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Create a Notification channel
13
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel =
new NotificationChannel(CHANNEL_ID, "Mascot Notification",
NotificationManager.IMPORTANCE_DEFAULT);
}
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Importance level
14
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification priority
setPriority(NotificationCompat.PRIORITY_HIGH)
15
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Importance level and priority constants
16
User-visible importance level | Importance (Android 8.0 and higher) | Priority (Android 7.1 and lower) |
Urgent Makes a sound and appears as a heads-up notification | ||
High Makes a sound | ||
Medium No sound | ||
Low No sound and doesn't appear in the status bar |
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Creating Notifications
17
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Creating Notification
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this, CHANNEL_ID);
18
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Setting notification contents
This is the only content that's required.
19
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Setting notification contents
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.");
20
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Tap action
and
Action buttons
21
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Add notification tap action
22
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notification action buttons
23
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Pending intents
24
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Methods to create a PendingIntent
To instantiate a PendingIntent, use one of the following methods:
25
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
PendingIntent method arguments
26
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Step 1: Create intent
Intent notificationIntent =
new Intent(this, MainActivity.class);
27
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Step 2: Create PendingIntent
PendingIntent notificationPendingIntent =
PendingIntent.getActivity(
this, � NOTIFICATION_ID,
notificationIntent, � PendingIntent.FLAG_UPDATE_CURRENT);
28
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Step 3: Add to notification builder
To set tap action to the notification:
.setContentIntent(notificationPendingIntent);
29
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Add action buttons
.addAction(R.drawable.ic_color_lens_black_24dp, � "R.string.label",� notificationPendingIntent);
30
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Expanded view notifications
31
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Expandable notifications
32
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Big text
33
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Big image
34
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Media
35
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Managing Notifications
Setting styles
To create expandable notification that appear, use one of the helper classes to set the style using the setStyle() method.
mNotifyBuilder
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(myBitmapImage)
.setBigContentTitle("Notification!"));
36
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Delivering Notifications
37
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Building a Notification
Delivering notifications
38
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Building a Notification
Instantiate NotificationManager
Call getSystemService(), passing in the NOTIFICATION_SERVICE constant.
mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
39
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Building a Notification
Send notification
mNotifyManager.notify(NOTIFICATION_ID, myNotification);
40
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Managing Notifications
41
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Managing Notifications
Updating notifications
42
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Managing Notifications
Canceling notifications
Notifications remain visible until:
mNotifyManager.cancel(NOTIFICATION_ID);
43
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Design guidelines
If your app sends too many notifications, users will disable notifications or uninstall the app.
44
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
What's Next?
45
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
The End
46
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.
Notifications
Android Developer Fundamentals V2
This work is licensed under a Creative Commons Attribution 4.0 International License.