Shaik Ahron
8 Dec 2022
•
2 min read
This is a new feature introduced in Android 11.
Using this feature you can communicate with people in a floating window.
Step 1: First, you need to create an instance of Person.
 val person = androidx.core.app.Person.Builder()
    .setName(simpleMessage.sender)
    .setIcon(icon)
    .setImportant(true)
    .build()
Step 2: Now, create an instance of BubbleMetadata.
  private fun createBubbleMetadata(
        contentUri: Uri,
        icon: IconCompat
    ): NotificationCompat.BubbleMetadata {
        val bubbleIntent =
            PendingIntent.getActivity(
                context,
                REQUEST_BUBBLE,
                Intent(context, MainActivity3::class.java)
                    .setAction(Intent.ACTION_VIEW)
                    .setData(contentUri),
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
            )
        val builder =  NotificationCompat.BubbleMetadata.Builder(bubbleIntent, icon)
        return builder
            .setDesiredHeightResId(R.dimen.bubble_height)
            .setAutoExpandBubble(true)
            .setSuppressNotification(true)
            .build()
    }
here as you can see we need the pendingIntent and icon they can’t be null for BubbleMetadata.you can set the height of the floating window.
Step 3: Create an instance of ShotcutInfoCompat.
ShotcutInfoCompat: this helps you access the features of ShortcutInfo.
A shortcut is basically an action that your app can perform and these shortcuts can be available in the phone launcher.
private fun createDynamicShortcut(
        message: SimpleMessage,
        icon: IconCompat,
        person: androidx.core.app.Person
    ): ShortcutInfoCompat {
        return ShortcutInfoCompat.Builder(context, message.id.toString())
            .setLongLived(true)
            .setIntent(
                Intent(context, MainActivity3::class.java)
                    .setAction(Intent.ACTION_VIEW)
                    .setData(createContentUri(message.text))
            )
            .setShortLabel(message.sender)
            .setIcon(icon)
            .setPerson(person)
            .build()
    }
Step 4: Now you need to publish this shortcut via ShortcutManager.
private fun addDynamicShortcut(shortcut: ShortcutInfo) {
        if (atLeastAndroid11()) {
            shortcutManager.pushDynamicShortcut(shortcut)
        } else {
            shortcutManager.addDynamicShortcuts(listOf(shortcut))
        }
}
Step 5: Now, create a notification builder and pass bubbleMetadata and shortcut id. you need to take care of backward compatibility as well.
private fun getNotificationBuilder(): NotificationCompat.Builder {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationCompat.Builder(context, CHANNEL_NEW_BUBBLE)
        } else {
            NotificationCompat.Builder(context)
        }
    }
with(builder) {
            setContentTitle(
                context.resources.getString(
                    R.string.message_from,
                    simpleMessage.sender
                )
            )
            setSmallIcon(R.drawable.ic_stat_notification)
            setCategory(android.app.Notification.CATEGORY_MESSAGE)
            setContentIntent(
                PendingIntent.getActivity(
                    context,
                    REQUEST_CONTENT,
                    Intent(context, MainActivity3::class.java)
                        .setAction(Intent.ACTION_VIEW)
                        .setData(contentUri),
                    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
                )
            )
            setShowWhen(true)
        }
Step 6: call the method by passing data.
BubbleNotificationView(context).showNotification(
    Message(
        123,
        "Arun",
        "Hello",
        R.drawable.ic_base_person
    )
)
Step 7: Make sure to add these properties resizeable and embedded. On devices running Android 10, notifications are not shown as bubbles unless you explicitly set documentLaunchMode to "always".
<activity
            android:name=".MainActivity3"
            android:allowEmbedded="true"
            android:resizeableActivity="true"
            android:documentLaunchMode="always"
            android:theme="@style/Theme.AppCompat.Light"
            android:exported="false">
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
Now, if you run the app.
https://gfycat.com/achingshorttermhoki.
Thank Your For Reading
Shaik Ahron
Hi, I am Shaik Ahron. I am enthusiastic about Mobile Development. I like to develop mobile apps.
See other articles by Shaik
Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ
108 E 16th Street, New York, NY 10003
Join over 111,000 others and get access to exclusive content, job opportunities and more!