ReCap: Android 11 People & Identity

The Android Developer site is providing cool learning pathways with their new ‘11 Weeks of Android‘. I’m going to recap each one here because Android development is one of my favorite topics.

Android 11 includes the Connection Notification API with their Google Identity Services Library for a better user signup / signin process.

The Android 11 OS recognizes and prioritizes the most important people in our lives with conversation notifications that show up at the top of the device screen with options that affect notification ranking and prioritization:

  • open conversation in a bubble
  • create conversation shortcut on home screen
  • set a reminder

Notifications

Notifications signal the UI to send short info about events in your app to the user: news alert, chat message, event reminder. They appear when your app is not in use. A few notification types include:

  • Bubble Notifications
  • People Notifications
  • Messaging Style Notifications
  • Heads-up Notifications

Add Support Library:

Use NotificationCompat APIs from the Android support library to add notification features for new devices while still being compatible for older devices to Android 4.0 (API level 14). Note, inline reply will not work on older devices. Add the following code to our build.gradle file:

dependencies {    
implementation "com.android.support:support-compat:28.0.0"
}

Create Basic Notification

Basic compact notification includes an icon, title and brief text.

Set Notification Content

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle(textTitle)
        .setContentText(textContent)
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

Android 8.0 (API Level 26) and higher requires the Channel ID while the older Android versions ignore it.

Enable Expandable Notification

var builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Much longer text that cannot fit one line...")
        .setStyle(NotificationCompat.BigTextStyle()
                .bigText("Much longer text that cannot fit one line..."))

        .setPriority(NotificationCompat.PRIORITY_DEFAULT)

Create Channel & Set Importance

Execute the following code as soon as your app starts because you must create the notification channel before posting notifications on Android 8.0 and higher. No operation is performed when creating a notification, so it is safe to call this repeatedly.

private fun createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val name = getString(R.string.channel_name)
        val descriptionText = getString(R.string.channel_description)
        val importance = NotificationManager.IMPORTANCE_DEFAULT
        val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
            description = descriptionText
        }
        // Register the channel with the system
        val notificationManager: NotificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }
}

Set Notification Tap Action

// Create an explicit intent for an Activity in your app
val intent = Intent(this, AlertDetails::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}

val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        // Set the intent that will fire when the user taps the notification
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

Show the Notification

Use the notification ID we passed in NotifcationManagerCompat.notify() – you will need it later if you want to update or remove the notification.

with(NotificationManagerCompat.from(this)) {
    // notificationId is a unique int for each notification that you must define
    notify(notificationId, builder.build())
}

Messaging Style Notifications

Messaging style notifications have a dedicated section in the drawer / shade area of the device screen and make it easier to share & allow actions with the person you are messaging.

One Tap & Google Identity Services

Reduce drop-off friction and improve user conversion rates with One API to bring the features of Google Sign-In and Smart Lock for Passwords into your Android apps. One Tap supports:

  • Sign in to existing account with your Android app service using their Google account or Google saved passwords.
  • Sign up for new account with your Android app service using their Google account (federated login)

Add the following to your dependencies: com.google.android.gms:play-services-auth with minimum version 18.0.0

Google Identity Services is a suite of libraries that will eventually include the full feature set of the Smart Lock for Passwords, Google Sign-In libraries and other new features.

One Tap is the 1st Google Identity Service feature that is ready to use for optimizing sign in & sign up flows. It can overlay any Activity in your Android application.

This concludes my notes for Android 11, People & Identity. Check out the resources for code repositories and more details.

Resources: