Download - Notifications - University of Arkansas
![Page 1: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/1.jpg)
Notifications
Alexander Nelson
October 4, 2021
University of Arkansas - Department of Computer Science and Computer Engineering
![Page 2: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/2.jpg)
Notifications
![Page 3: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/3.jpg)
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.
![Page 4: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/4.jpg)
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
![Page 5: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/5.jpg)
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
![Page 6: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/6.jpg)
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
![Page 7: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/7.jpg)
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)
![Page 8: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/8.jpg)
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()
![Page 9: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/9.jpg)
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
![Page 10: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/10.jpg)
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
![Page 11: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/11.jpg)
Build Simple Notification
Create explicit intent for the activity “ResultActivity.class”
.setChannelId() for API 26 and later
![Page 12: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/12.jpg)
Build Simple Notification
Create artificial back stack navigating to the correct activity
.setContentIntent() chooses the click action
![Page 13: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/13.jpg)
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
![Page 14: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/14.jpg)
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
![Page 15: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/15.jpg)
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
![Page 16: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/16.jpg)
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
![Page 17: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/17.jpg)
Updating a Notification
![Page 18: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/18.jpg)
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
![Page 19: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/19.jpg)
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
![Page 20: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/20.jpg)
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
![Page 21: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/21.jpg)
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
![Page 22: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/22.jpg)
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
![Page 23: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/23.jpg)
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()
![Page 24: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/24.jpg)
Example Create Back Stack Automatically
If each activity has it’s parent defined:
![Page 25: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/25.jpg)
Example Create Back Stack Manually
![Page 26: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/26.jpg)
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
![Page 27: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/27.jpg)
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()
![Page 28: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/28.jpg)
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
![Page 29: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/29.jpg)
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()
![Page 30: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/30.jpg)
Example Special Activity
![Page 31: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/31.jpg)
Example Special Activity
![Page 32: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/32.jpg)
Notification Options
![Page 33: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/33.jpg)
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+)
![Page 34: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/34.jpg)
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
![Page 35: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/35.jpg)
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
![Page 36: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/36.jpg)
Reply to a Notification
API level 24 (Nougat) and higher
Users can respond directly to
notifications inside the dialog
using inline actions
![Page 37: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/37.jpg)
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
![Page 38: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/38.jpg)
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
![Page 39: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/39.jpg)
Bundling Notifications
Also API 24 (Nougat), notifications may be bundled to represent a
queue of notifications
Use Bulder.setGroup() to bundle similar notifications
![Page 40: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/40.jpg)
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
![Page 41: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/41.jpg)
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
![Page 42: Notifications - University of Arkansas](https://reader031.vdocuments.us/reader031/viewer/2022011913/61d79c82da446c5f904dfdc7/html5/thumbnails/42.jpg)
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