Background Bubbles
But what if the app is in the background when it launches a bubble?
An app can launch a bubble even when it isn’t in the foreground. However, the bubble won’t expand on all devices. To see this in action, open QuoteViewModel.kt. Find showAsBubble
and replace the body with:
val scope = CoroutineScope(Dispatchers.Main)
scope.launch {
delay(5000)
quoteData.value?.let { quote ->
category.value?.let { category ->
QuoteAndCategory(quote, category)
}
}?.let { repository.showAsBubble(it) }
}
The code above will wait five seconds before launching the notification bubble. Build and run and select a category that you’ve previously allowed to bubble. Press the icon to bubble the conversation, then quickly put the app in the background:
Soon, you’ll see the bubble appear, even though the app isn’t in the foreground. Depending on your device, it may or may not be expanded.
Where to Go From Here?
Download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.
In this tutorial, you learned how to turn a normal notification into a bubble and link directly to your app via sharing and shortcuts.
If you want to learn more about bubbles, visit the Official Android Documentation.
To learn more about notifications, check out this lesson from the Android Background Processing video course.
For a Codelab about shortcuts and bubbles, check out People: Conversations and Bubbles.
To learn more about direct share and shortcuts, see this Codelab.
I hope you enjoyed this tutorial! If you have any questions or comments “bubble up”, please join the forum discussion below.