notifications - university of arkansas
TRANSCRIPT
Notifications
Alexander Nelson
October 4, 2021
University of Arkansas - Department of Computer Science and Computer Engineering
Notifications
Notifications
A notification is a message which is displayed to the user outside
of the normal User Interface
https://developer.android.com/guide/topics/ui/notifiers/notifications.html
These slides are modifications based on work created and shared by the
Android Open Source Project and used according to terms described in the
Creative Commons 2.5 Attribution License.
Notifications
When a notification is issued, it appears
in the notification area
To see details, a user may open the
notification drawer
Both the notification area and drawer
are system controlled, and the user may
view them at any time
Notification Design
Notifications have their own design style guide:
https://material.io/design/platform-guidance/android-
notifications.html
This guide has a lot of really good information from a UI design
perspective that can be consulted
Creating a Notification
UI information and actions are determined in a
NotifiactionCompat.Builder object
Created using the Builder.build() function, which returns a
Notification object with the specifications
Issued using the NotificationManager.notify() method
Required Contents
A notification must contain the following:
• setSmallIcon() – sets the small icon
• setContentTitle() – Title for the notification
• setContentText() – sets the detail text
• setChannelId() – set a channel (for API 26 and above)
Optional Notification Contents
Action – Allow directly placing the user in the Activity that the
notification addresses
• Defined by a PendingIntent that starts an Activity
• If not the home screen, create an artificial back stack
Click Behavior – Associate a PendingIntent with a click/swipe
behavior
• Set click behavior with setContentIntent()
• Set swipe or delete notification with setDeleteIntent()
Notification priority
Priorities based on channels after API 26
Use NotificationChannel.setImportance(int)
Importance Values:
• IMPORTANCE UNSPECIFIED
• IMPORTANCE NONE
• IMPORTANCE MIN
• IMPORTANCE LOW
• IMPORTANCE DEFAULT
• IMPORTANCE HIGH
Optional Notification Contents
• Sounds – setSound()
• Color – setColorized() - should only be used for high priority
ongoing tasks
• setFullScreenIntent() – Can be used to directly launch app.
Very disruptive if not needed
• setLights() – Used to manipulate LED on device
• More on reference page for NotificationCompat.Builder
Build Simple Notification
Create explicit intent for the activity “ResultActivity.class”
.setChannelId() for API 26 and later
Build Simple Notification
Create artificial back stack navigating to the correct activity
.setContentIntent() chooses the click action
Build Simple Notification
Get instance of the Notification Manager
.build() builds the notification from the Builder object
NotificationManager instance’s .notify() method shows the
notification
Expanded Layouts
You may want to use larger notifications for specific purposes
The following table describes a few:
Style Description
BigPictureStyle Notifications that include a large
image attachment
BigTextStyle Notifications that include a lot of
text
DecoratedCustomViewStyle Custom views that are decorated
by the system
InboxStyle Nnotifications that include a list
of (up to 5) strings
More at https://developer.android.com/reference/
android/support/v4/app/NotificationCompat.Style.html
Updating a Notification
Often better to update an existing notification rather than issue a
new one
To update, issue notifications using the same notification ID
If a notification exists with that ID, the notification is updated
Updating a Notification
Android limits the rate at which notifications may be updated
Posting updates too frequently can result in dropped notifications
setOnlyAlertOnce() – Allow providing sound/vibrate/ticker for
notifications on just the original notification, not updates
Updating a Notification
Removing a Notification
Notifications remain visible until:
• User dismisses the notification (individually or clear all)
• User clicks notification, and setAutoCancel() is specified for
notification
• cancel() is called for the notification ID
• cancelAll() is called, removing all notifications issued by the
application
• setTimeoutAfter() – System cancels notification after a
duration elapses
Preserving Navigation
When an activity is started from a notification, you should consider
the user’s expected experience
e.g. If you start a second layer activity, pressing back should return
to the main application activity
Two general situations: Regular Activity, Special Activity
Regular Activity
If starting activity that is part of App’s normal workflow:
• set PendingIntent() to start fresh task
• provide PendingIntent() with back stack reproducing the
normal back behavior
Setting up Regular Activity PendingIntent
To provoide a PendingIntent that starts a direct Activity:
1. Define Activity hierarchy in manifest
2. Create back stack based on the Intent that starts the Activity
Defining Hierarchy
For Android 4.0.3 and lower:
• Specify parent by adding a <meta-data>
• Parameters are:
• android:name=“android.support.PARENT ACTIVITY”
• android:value=“.MainActivity”
For Android 4.1 and later:
• Add android:parentActivityName tag
Create back stack
To create the back stack for the activity
• Create the Intent to start the Activity
• Create a stack builder object
• Add back stack to stack builder by calling addParentStack()
• Add the Intent that starts the Activity with addNextIntent()
• Add any arguments to the Intent objects with .editIntentAt()
• Get a PendingIntent for the backstack by calling
getPendingIntent()
Example Create Back Stack Automatically
If each activity has it’s parent defined:
Example Create Back Stack Manually
Special Activity
For an Activity that only occurs through a notification, it doesn’t
make sense to return to the application
In this case, pressing back should return to the Home screen
Set up Special Activity PendingIntent
Special Activity doesn’t need a back stack
No need to define its parent in the manifest
Instead, get PendingIntent by calling getActivity()
Special Activity Manifest
Special parameters:
• android:launchMode=“singleTask”
• android:taskAffinity=“”
• android:excludeFromRecents=“true”
These three make sure that the activity is launched and not the
default, and that it wont show up in the recent applications so user
can’t navigate back to the special activity
Build and Issue Notification
To build the notification:
1. Create an Intent to start the Activity
2. Set the Activity to start in a new, empty task, by callingsetFlags() with:
• FLAG ACTIVITY NEW TASK
• FLAG ACTIVITY CLEAR TASK
3. Set any other options
4. Create PendingIntent by calling getActivity()
Example Special Activity
Example Special Activity
Notification Options
Heads-Up Notification
Notifications can briefly appear in a floating window
For actions the user should know about immediately
Triggering Actions:
• Foreground activity is in fullscreen mode
• Notification has high priority & uses ringtones/vibration (API
25 and lower)
• Notification channel has high importance (API 26+)
Lock Screen
Notifications can appear on the lock screen
Notifications should programattically define level of detail available
on the lock screen
API 26+, users can choose to disable/enable lock screen
notifications for each channel
More information - Lock Screen Visibility
Badges
In supported launchers running on devices of Android 8.0 (API 26:
Oreo), apps may:
• Display notification badges on app icons
• Long-press on app icon to glance at notifications associated
with a badge
Reply to a Notification
API level 24 (Nougat) and higher
Users can respond directly to
notifications inside the dialog
using inline actions
Add Inline Reply Action
To enable this behavior:
1. Create instance of RemoteInput.Builder
2. Attach RemoteInput object to an action with
addRemoteInput()
3. Apply the action to a notification and issue the notification
Retrieve user input from Inline Reply
If user chooses reply action, you get the reply by:
1. getResultsFromIntent(intent) – Returns a Bundle that
contains the text response
2. Query the bundle using the result key
3. Build and issue another notification to show that reply action
succeeded
Bundling Notifications
Also API 24 (Nougat), notifications may be bundled to represent a
queue of notifications
Use Bulder.setGroup() to bundle similar notifications
Bundling Notifications
These notification groups provide
a parent notification, which may
be expanded to show child
notifications
These child notifications may in
turn be expanded to show its
entire content
If the same app sends four or more notification with no group,
Android will automatically bundle them
When to Bundle
Bundle notifications if the following are true:
• Child notifications are complete notifications – Can be
displayed without need of group summary
• There is a benefit to surfacing the child notification, e.g.
• They are actionable, and actions are specific to a child
• There is more information to the child that may need to be
read
Notification Channels
Starting with Android 8.0, notification channels allow creation of
user-customizable channel for each type of notification
Allows user specification of channels to determine how a
notification is shown