0% found this document useful (0 votes)
13 views

08.1 Notifications

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

08.1 Notifications

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

Android Developer Fundamentals V2

Alarms and
Schedulers
Lesson 8

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 1
Fundamentals V2 International License
8.1 Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 2
Fundamentals V2 International License
Contents

● What are notifications? ● Tap action and action


● Notification channels buttons

● Creating a notification ● Expanded view


channel notifications

● Creating notifications ● Delivering notifications


● Managing Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 3
Fundamentals V2 International License
What Are
Notifications
?

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 4
Fundamentals V2 International License
What is a notification?

Message displayed to user outside regular app UI

■ Small icon
■ Title
■ Detail text

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 5
Fundamentals V2 International License
How are notifications used?
● Android issues a notification that
appears as icon on the status bar.
● To see details, user opens the
notification drawer.
● User can view notifications any time in
the notification drawer.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 6
Fundamentals V2 International License
App icon badge
Available only on the devices running Android 8.0 (API level 26)
and higher.
● New notifications are displayed as a colored
"badge" (also known as a "notification dot") on
the app icon.

● Users can long-press on an app icon to


see the notifications for that app. Similar
to the notification drawer.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 7
Fundamentals V2 International License
Notification
Channels

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 8
Fundamentals V2 International License
Notification channels

● Used to create a user-customizable channel for


each type of notification to be displayed.
● More than one notification can be grouped in to a
channel.
● Set notification behavior like sound, light, vibrate
and so on, applied to all the notifications in that
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 9
Fundamentals V2 International License
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. This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 10
Fundamentals V2 International License
Notification channels in
Settings
● Notification channels
appear as Categories
under App notifications
in the device Settings.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 11
Fundamentals V2 International License
Creating a
Notification
channel

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 12
Fundamentals V2 International License
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.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {


NotificationChannel notificationChannel =
new NotificationChannel(CHANNEL_ID, "Mascot Notification",
NotificationManager.IMPORTANCE_DEFAULT);
This work is licensed under a
} Android Developer Notifications Creative Commons Attribution 4.0 13
Fundamentals V2 International License
Importance level
● Available in Android 8.0 (API level 26) and higher.
● Sets the intrusion level, like the sound and visibility for
all notifications posted in the channel.
● Range from IMPORTANCE_NONE(0) to
IMPORTANCE_HIGH(4).
● To support earlier versions of Android (Lower than API
level 26), set the priority.
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 14
Fundamentals V2 International License
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)
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 15
Fundamentals V2 International License
Importance level and priority
constants Importance
Priority (Android
User-visible importance level (Android 8.0 and
7.1 and lower)
higher)
Urgent
PRIORITY_HIGH or
Makes a sound and appears as a heads- IMPORTANCE_HIGH
PRIORITY_MAX
up notification
High
IMPORTANCE_DEFAUL PRIORITY_DEFAULT
Makes a sound T
Medium
IMPORTANCE_LOW PRIORITY_LOW
No sound
Low
No sound and doesn't appear in the IMPORTANCE_MIN PRIORITY_MIN
status bar
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 16
Fundamentals V2 International License
Creating
Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 17
Fundamentals V2 International License
Creating Notification
● 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


This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 18
NotificationCompat.Builder(this, CHANNEL_ID);
Fundamentals V2 International License
Setting notification contents
1. A small icon, set by
setSmallIcon().
This is the only content that's
required.
2. A title, set by setContentTitle().
3. The body text, set by
setContentText(). This is the
notification message.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 19
Fundamentals V2 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.");

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 20
Fundamentals V2 International License
Tap action
and
Action
buttons

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 21
Fundamentals V2 International License
Add notification tap action
● Every notification must respond when it is tapped,
usually launching an Activity in your app.
● Set an content intent using setContentIntent()
method.
● Pass the Intent wrapped in a PendingIntent object.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 22
Fundamentals V2 International License
Notification action buttons

● Action buttons can perform a variety of actions on


behalf of your app, such as starting a background
task, placing a phone call and so on.
● Starting from Android 7.0 (API level 24) reply to
messages directly from notifications.
● To add an action button, pass a PendingIntent to
the addAction() method.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 23
Fundamentals V2 International License
Pending intents

● A PendingIntent is a description of an intent and


target action to perform with it.

● Give a PendingIntent to another application to


grant it the right to perform the operation you
have specified as if the other app was yourself.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 24
Fundamentals V2 International License
Methods to create a
PendingIntent
To instantiate a PendingIntent, use one of the following
methods:
● PendingIntent.getActivity()
● PendingIntent.getBroadcast()
● PendingIntent.getService()

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 25
Fundamentals V2 International License
PendingIntent method
arguments
1. Application context
2. Request code—constant integer id for the pending
intent
3. Intent to be delivered
4. PendingIntent flag determines how the system
handles multiple pending intents from same app

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 26
Fundamentals V2 International License
Step 1: Create intent

Intent notificationIntent =
new Intent(this, MainActivity.class);

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 27
Fundamentals V2 International License
Step 2: Create PendingIntent

PendingIntent notificationPendingIntent =
PendingIntent.getActivity(
this,
NOTIFICATION_ID,
notificationIntent,

PendingIntent.FLAG_UPDATE_CURRENT);
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 28
Fundamentals V2 International License
Step 3: Add to notification
builder
To set tap action to the notification:

.setContentIntent(notificationPendingIntent);

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 29
Fundamentals V2 International License
Add action buttons
● Use NotificationCompat.Builder.addAction()
— pass in icon, caption, PendingIntent

.addAction(R.drawable.ic_color_lens_black_24d
p,
"R.string.label",
notificationPendingIntent);
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 30
Fundamentals V2 International License
Expanded
view
notificatio
ns

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 31
Fundamentals V2 International License
Expandable notifications
● Notifications in the notification drawer appear in
two main layouts, normal view (which is the
default) and expanded view.
● Expanded view notifications were introduced in
Android 4.1.
● Use them sparingly -- they take up more space and
attention.
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 32
Fundamentals V2 International License
Big text

● For large-format notifications that


include a lot of text.
● Fits more text than a standard
view.
● Use the helper class:
NotificationCompat.BigTextSt
yle
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 33
Fundamentals V2 International License
Big image

● For large-format notifications


that include a large image
attachment.

● Use the helper class:


NotificationCompa.BigPictureStyle

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 34
Fundamentals V2 International License
Media

● For media playback notifications.


● Actions for controlling media
such as music
● Image for album cover
● Use the helper class:
● NotificationCompat.MediaStyle
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 35
Fundamentals V2 International License
Managing
Setting styles
Notifications
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!")); This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 36
Fundamentals V2 International License
Delivering
Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 37
Fundamentals V2 International License
Building a Notification
Delivering notifications

● Use the NotificationManager class to deliver


notifications.
○ Create an instance of NotificationManager
○ Call notify() to deliver the notification.

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 38
Fundamentals V2 International License
Building a Notification
Instantiate NotificationManager

Call getSystemService(), passing in the NOTIFICATION_SERVICE


constant.

mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 39
Fundamentals V2 International License
Building
Send notification
a Notification
● Call notify()to deliver the notification, passing in
these two values:
○ A notification ID, which is used to update or cancel the
notification.
○ The NotificationCompat object that you created using the
NotificationCompat.Builder object.

mNotifyManager.notify(NOTIFICATION_ID, myNotification);

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 40
Fundamentals V2 International License
Managing
Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 41
Fundamentals V2 International License
Managingnotifications
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.
Android Developer
This work is licensed under a
Creative Commons Attribution 4.0
Notifications 42
Fundamentals V2 International License
Managing notifications
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.
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 43
Fundamentals V2 International License
Design guidelines
If your app sends too many notifications, users will
disable notifications or uninstall the app.
● Relevant: Whether this information is essential for
the user.
● Timely: Notifications need to appear when they are
useful.
● Short: Use as few words as possible.
● Give users the power to choose -- Use appropriate
This work is licensed under a
Android Developer Notifications Creative Commons Attribution 4.0 44
Fundamentals V2 International License
What's Next?

● Concept Chapter: 8.1 Notifications


● Practical: 8.1 Notifications

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 45
Fundamentals V2 International License
The End

This work is licensed under a


Android Developer Notifications Creative Commons Attribution 4.0 46
Fundamentals V2 International License

You might also like