I am handling a chat app with a disabled preview message feature, so when receive a new message, I show a bubble with the text "You have a new message".
In the first time it work fine, but after user close this bubble then it is not shown again, there are nothings in the status bar also and i just see the unread badge in launcher icon.
**Note: **This case is opcurs in android 12 and 13, I have tested in android 11 and it still work fine.
I think there is a rule or the issue of OS but i dont find any document about this. I also to try with (sameple bubble chat app of google but nothing diffenrent.
Please give me some document or information about this issue, thank in advance!
This is my code to show bubble chat:
fun showNotification(chat: Chat, fromUser: Boolean, update: Boolean = false) {
updateShortcuts(chat.contact)
val icon = IconCompat.createWithAdaptiveBitmapContentUri(chat.contact.iconUri)
val user = Person.Builder().setName(context.getString(R.string.sender_you)).build()
val person = Person.Builder().setName(chat.contact.name).setIcon(icon).build()
val contentUri = "https://android.example.com/chat/${chat.contact.id}".toUri()
val pendingIntent = PendingIntent.getActivity(
context,
REQUEST_BUBBLE,
// Launch BubbleActivity as the expanded bubble.
Intent(context, BubbleActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setData(contentUri),
flagUpdateCurrent(mutable = true)
)
// Let's add some more content to the notification in case it falls back to a normal
// notification.
val messagingStyle = NotificationCompat.MessagingStyle(user)
val lastId = chat.messages.last().id
for (message in chat.messages) {
val m = NotificationCompat.MessagingStyle.Message(
"You have a new message",
message.timestamp,
if (message.isIncoming) person else null
).apply {
if (message.photoUri != null) {
setData(message.photoMimeType, message.photoUri)
}
}
if (message.id < lastId) {
messagingStyle.addHistoricMessage(m)
} else {
messagingStyle.addMessage(m)
}
}
val builder = NotificationCompat.Builder(context, CHANNEL_NEW_MESSAGES)
// A notification can be shown as a bubble by calling setBubbleMetadata()
.setBubbleMetadata(
NotificationCompat.BubbleMetadata.Builder(pendingIntent, icon)
// The height of the expanded bubble.
.setDesiredHeight(context.resources.getDimensionPixelSize(R.dimen.bubble_height))
.apply {
// When the bubble is explicitly opened by the user, we can show the bubble
// automatically in the expanded state. This works only when the app is in
// the foreground.
if (fromUser) {
setAutoExpandBubble(true)
}
if (fromUser || update) {
setSuppressNotification(true)
}
}
.build()
)
// The user can turn off the bubble in system settings. In that case, this notification
// is shown as a normal notification instead of a bubble. Make sure that this
// notification works as a normal notification as well.
.setContentTitle(chat.contact.name)
.setSmallIcon(R.drawable.ic_message)
.setCategory(Notification.CATEGORY_MESSAGE)
.setShortcutId(chat.contact.shortcutId)
// This ID helps the intelligence services of the device to correlate this notification
// with the corresponding dynamic shortcut.
.setLocusId(LocusIdCompat(chat.contact.shortcutId))
.addPerson(person)
.setShowWhen(true)
// The content Intent is used when the user clicks on the "Open Content" icon button on
// the expanded bubble, as well as when the fall-back notification is clicked.
.setContentIntent(
PendingIntent.getActivity(
context,
REQUEST_CONTENT,
Intent(context, MainActivity::class.java)
.setAction(Intent.ACTION_VIEW)
.setData(contentUri),
flagUpdateCurrent(mutable = false)
)
)
// Direct Reply
.addAction(
NotificationCompat.Action
.Builder(
IconCompat.createWithResource(context, R.drawable.ic_send),
context.getString(R.string.label_reply),
PendingIntent.getBroadcast(
context,
REQUEST_CONTENT,
Intent(context, ReplyReceiver::class.java).setData(contentUri),
flagUpdateCurrent(mutable = true)
)
)
.addRemoteInput(
RemoteInput.Builder(ReplyReceiver.KEY_TEXT_REPLY)
.setLabel(context.getString(R.string.hint_input))
.build()
)
.setAllowGeneratedReplies(true)
.build()
)
// Let's add some more content to the notification in case it falls back to a normal
// notification.
.setStyle(messagingStyle)
.setWhen(chat.messages.last().timestamp)
// Don't sound/vibrate if an update to an existing notification.
if (update) {
builder.setOnlyAlertOnce(true)
}
notificationManager.notify(chat.contact.id.toInt(), builder.build())
}