1 of 46

Alarms and Schedulers

Lesson 8

Android Developer Fundamentals V2

1

1

1

Notifications

Android Developer Fundamentals V2

Notifications

Android Developer Fundamentals V2

2 of 46

8.1 Notifications

2

Notifications

Android Developer Fundamentals V2

3 of 46

Contents

  • What are notifications?
  • Notification channels
  • Creating a notification channel
  • Creating notifications

  • Tap action and action buttons
  • Expanded view notifications
  • Delivering notifications
  • Managing Notifications

3

Notifications

Android Developer Fundamentals V2

4 of 46

What Are Notifications?

4

Notifications

Android Developer Fundamentals V2

5 of 46

What is a notification?

Message displayed to user outside regular app UI

                  • Small icon
                  • Title
                  • Detail text

5

Notifications

Android Developer Fundamentals V2

6 of 46

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.

6

Notifications

Android Developer Fundamentals V2

7 of 46

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.

7

Notifications

Android Developer Fundamentals V2

8 of 46

Notification

Channels

8

Notifications

Android Developer Fundamentals V2

9 of 46

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.

9

Notifications

Android Developer Fundamentals V2

10 of 46

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.

10

Notifications

Android Developer Fundamentals V2

11 of 46

Notification channels in Settings

  • Notification channels appear as Categories under App notifications in the device Settings.

11

Notifications

Android Developer Fundamentals V2

12 of 46

Creating a Notification channel

12

Notifications

Android Developer Fundamentals V2

13 of 46

Create a Notification channel

13

  • 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);

}

Notifications

Android Developer Fundamentals V2

14 of 46

Importance level

14

  • 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.

Notifications

Android Developer Fundamentals V2

15 of 46

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)

15

Notifications

Android Developer Fundamentals V2

16 of 46

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

17 of 46

Creating Notifications

17

Notifications

Android Developer Fundamentals V2

18 of 46

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);

18

Notifications

Android Developer Fundamentals V2

19 of 46

Setting notification contents

  1. A small icon, set by setSmallIcon().

This is the only content that's required.

19

Notifications

Android Developer Fundamentals V2

20 of 46

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

21 of 46

Tap action

and

Action buttons

21

Notifications

Android Developer Fundamentals V2

22 of 46

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.

22

Notifications

Android Developer Fundamentals V2

23 of 46

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.

23

Notifications

Android Developer Fundamentals V2

24 of 46

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.

24

Notifications

Android Developer Fundamentals V2

25 of 46

Methods to create a PendingIntent

To instantiate a PendingIntent, use one of the following methods:

25

Notifications

Android Developer Fundamentals V2

26 of 46

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

26

Notifications

Android Developer Fundamentals V2

27 of 46

Step 1: Create intent

Intent notificationIntent =

new Intent(this, MainActivity.class);

27

Notifications

Android Developer Fundamentals V2

28 of 46

Step 2: Create PendingIntent

PendingIntent notificationPendingIntent =

PendingIntent.getActivity(

this, � NOTIFICATION_ID,

notificationIntent, � PendingIntent.FLAG_UPDATE_CURRENT);

28

Notifications

Android Developer Fundamentals V2

29 of 46

Step 3: Add to notification builder

To set tap action to the notification:

.setContentIntent(notificationPendingIntent);

29

Notifications

Android Developer Fundamentals V2

30 of 46

Add action buttons

  • Use NotificationCompat.Builder.addAction()�— pass in icon, caption, PendingIntent

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

30

Notifications

Android Developer Fundamentals V2

31 of 46

Expanded view notifications

31

Notifications

Android Developer Fundamentals V2

32 of 46

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.

32

Notifications

Android Developer Fundamentals V2

33 of 46

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

33

Notifications

Android Developer Fundamentals V2

34 of 46

Big image

  • For large-format notifications that include a large image attachment.

  • Use the helper class:

34

Notifications

Android Developer Fundamentals V2

35 of 46

Media

  • For media playback notifications.
  • Actions for controlling media�such as music
  • Image for album cover
  • Use the helper class:
  • NotificationCompat.MediaStyle

35

Notifications

Android Developer Fundamentals V2

36 of 46

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

37 of 46

Delivering Notifications

37

Notifications

Android Developer Fundamentals V2

38 of 46

Building a Notification

Delivering notifications

  • Use the NotificationManager class to deliver notifications.
    • Create an instance of NotificationManager
    • Call notify() to deliver the notification.

38

Notifications

Android Developer Fundamentals V2

39 of 46

Building a Notification

Instantiate NotificationManager

Call getSystemService(), passing in the NOTIFICATION_SERVICE constant.

mNotifyManager = (NotificationManager)

getSystemService(NOTIFICATION_SERVICE);

39

Notifications

Android Developer Fundamentals V2

40 of 46

Building a Notification

Send 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);

40

Notifications

Android Developer Fundamentals V2

41 of 46

Managing Notifications

41

Notifications

Android Developer Fundamentals V2

42 of 46

Managing Notifications

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.
  4. If previous notification is still visible, system updates.
  5. If previous notification has been dismissed, new notification is delivered.

42

Notifications

Android Developer Fundamentals V2

43 of 46

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);

43

Notifications

Android Developer Fundamentals V2

44 of 46

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.

44

Notifications

Android Developer Fundamentals V2

45 of 46

What's Next?

45

  • Concept Chapter: 8.1 Notifications
  • Practical: 8.1 Notifications

Notifications

Android Developer Fundamentals V2

46 of 46

The End

46

Notifications

Android Developer Fundamentals V2

Notifications

Android Developer Fundamentals V2