Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Developer Guide to Implement Bubble API in Android Application

Introduction

Google recently introduced bubbles API in 2019 for display Notification. In Android Q, this has made chat head notifications part of the Android platform as bubble API. This bubble notification feature was introduced first for Pixel devices only and worked similar to the Facebook messenger chat bubble. In Android 10, this wasn’t a stable release, and users had to enable it from developer options in their settings.

Bubble API’s final version came with Android 11. Bubbles are a preview feature in Android 11 that helps you perform more than one task or use more than one application at a time. Android has a native implementation to make conversations easier and more accessible to the user. They are built into the notification system and float on top of other apps.

In this blog, we will learn how to use bubble notifications in our application. So, get started.

Bubble notifications only appear when it meets the conversation requirement. So here are a few limitations for when notifications can be shown as bubbles.

Limitations in Android 10

⦁ For Notifications, we use Messingingstyle, and a Person added

⦁ When notifications are sent, the app must be in the foreground

Limitations on Android 11

⦁ Notifications use MessagingStyle

Guidelines

Conversation Requirements

⦁ First, to fulfil the notification conversation requirements – we need to implement long-lived dynamic or sharing shortcuts and attach the shortcut id to your messaging style notification.

How are Bubbles implemented?

1. Check SDK version

  •  The first step is to ensure that our application targets Android 11 (API Level 30).
Android {
	    compileSdkVersion 30
	    buildToolsVersion "30.0.2"

	    defaultConfig {
	        targetSdkVersion 30
	    }
	}

2. Configure Activity

<activity
 	  Android:name=".bubbles.BubbleActivity"
	  Android:theme="@style/AppTheme.NoActionBar"
	  Android:label="@string/title_activity_bubble"
 	  Android:allowEmbedded="true"
 	  Android:resizeableActivity="true"
	/>
  • This activity is displayed as a bubble, and it must be resizable and embedded.
  • So, in Android 10, notifications are not displayed as bubbles unless you set documentLaunchMode = “always” in the manifest file. However, in Android 11, there is no need to set this value because the system automatically sets all conversations’ documentLaunchMode to “always.”

3. Create Intent

  • We must create a PendingIntent that navigates to bubble activity.
val screen = Intent(context, BubblesActivity::class.java)
val bubbleInt = PendingIntent.getActivity(context, 0, screen, 0 )

4. Create MetaData

  • We need to create metadata and a person objects to pass in the notification.
Val bubbleData=Notification.BubbleMetadata.Builder(bubbleInt ,
 Icon.createWithResource(context, R.drawable.icon))
.setAutoExpandBubble(true)
.setDesiredHeight(600).build()
  • When you click on a bubble, it shows the content, but you can always auto-expand the bubble using this method setAutoExpand(true).

5. Create Person

  • We have to create person object with the information about a chat partner.
val chatPartner = Person.Builder()
    	.setName("Chat partner")
            .setImportant(true)
   	.build()

6. Create Shortcut

val shortcut =ShortcutInfo.Builder(mContext, shortcutId)
      	.setIntent(Intent(Intent.ACTION_DEFAULT))
     	.setLongLived(true)
      	.setShortLabel(chatPartner.name)
      	.build()

8. Create Notification

  • Finally, create the notification and set the metadata using the method setBubbleMetadata.
val builder = Notification.Builder(context, CHANNEL_ID)
    	  .setContentIntent(contentIntent)
    	  .setSmallIcon(smallIcon)
    	  .setBubbleMetadata(bubbleData)
    	  .setShortcutId(shortcutId)
    	  .addPerson(chatPartner)
  • When setting normal values in the notification builder the user can turn off bubbles from the system setting, and then the normal notification will be shown instead of bubbles.
with(builder) {
   		   setContentTitle("New message")
 		   setSmallIcon(R.drawable.ic_stat_notification)
 		   setCategory(Notification.CATEGORY_MESSAGE)
   		  setContentIntent(
      		  PendingIntent.getActivity(   context,    0,
            	  Intent(context, MainActivity::class.java),
           		   PendingIntent.FLAG_UPDATE_CURRENT
      		  )
    		)
	}
//then call the notification manager to send it
	notificationManager.notify(MY_NOTIFICATION, builder.build())

Conclusion

By following the above steps, you would have learned to properly implement bubble-style notifications in your application. The blog also stated the important points that should be kept in mind while implementing bubble notifications. If you have any doubts about this topic, please feel free to drop a comment, and our DEV IT engineers will get back to you.

The post Developer Guide to Implement Bubble API in Android Application appeared first on DEV IT Journal.



This post first appeared on DEV IT Journal - Simplifying IT, Empowering Busine, please read the originial post: here

Share the post

Developer Guide to Implement Bubble API in Android Application

×

Subscribe to Dev It Journal - Simplifying It, Empowering Busine

Get updates delivered right to your inbox!

Thank you for your subscription

×