0% found this document useful (0 votes)
56 views47 pages

Notifications

This document discusses Android notifications. It covers what notifications are, notification channels, creating notification channels, and creating notifications. Notifications display messages to users outside of an app's main UI. Notification channels allow grouping notifications and customizing notification settings. All notifications must be assigned to a channel on Android 8.0 and higher. The document provides code samples for creating notification channels and notifications.

Uploaded by

ayubniazi1976
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)
56 views47 pages

Notifications

This document discusses Android notifications. It covers what notifications are, notification channels, creating notification channels, and creating notifications. Notifications display messages to users outside of an app's main UI. Notification channels allow grouping notifications and customizing notification settings. All notifications must be assigned to a channel on Android 8.0 and higher. The document provides code samples for creating notification channels and notifications.

Uploaded by

ayubniazi1976
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/ 47

Android Developer Fundamentals V2

Alarms and
Schedulers
Lesson 8

This work is licensed under a


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

This work is licensed under a


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

● What are notifications? ● Tap action and action buttons


● Notification channels ● Expanded view notifications
● Creating a notification channel ● Delivering notifications
● Creating notifications ● Managing Notifications

This work is licensed under a


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

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 4
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 5
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 6
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 7
national License
Notification
Channels

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 8
national 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 channel.

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 9
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 10
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 11
national License
Creating a
Notification
channel

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 12
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 13
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 14
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 15
national License
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_DEFAUL PRIORITY_DEFAULT
Makes a sound T
Medium
IMPORTANCE_LOW PRIORITY_LOW
No sound
Low
No sound and doesn't appear in the status bar IMPORTANCE_MIN PRIORITY_MIN

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 16
national License
Creating
Notifications

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 17
national 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


NotificationCompat.Builder(this, CHANNEL_ID);

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 18
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 19
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 20
national License
link
1. https://www.youtube.com/watch?v=tTbd1Mfi-Sk

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 21
national License
Tap action
and
Action buttons

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 22
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 23
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 24
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 25
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 26
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 27
national License
Step 1: Create intent

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

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 28
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 29
national License
Step 3: Add to notification builder

To set tap action to the notification:

.setContentIntent(notificationPendingIntent);

This work is licensed under a


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

.addAction(R.drawable.ic_color_lens_black_24dp,
"R.string.label",
notificationPendingIntent);

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 31
national License
Expanded
view
notifications

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 32
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 33
national 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.BigTextStyle

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 34
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 35
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 36
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 37
national License
Delivering
Notifications

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 38
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 39
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 40
national 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 Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 41
national License
Managing
Notifications

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 42
national 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.

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 43
national 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.

mNotifyManager.cancel(NOTIFICATION_ID);
This work is licensed under a
Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 44
national 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 notification
channels to categorise your notifications.
This work is licensed under a
Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 45
national License
What's Next?

● Concept Chapter: 8.1 Notifications


● Practical: 8.1 Notifications

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 46
national License
The End

This work is licensed under a


Android Developer Fundamentals V2 Notifications Creative Commons Attribution 4.0 Inter 47
national License

You might also like