Search code examples
android-11android-bubbles

setShortcutInfo in NotificationCompat.Builder and pushDynamicShortcut in ShortcutManagerCompat doesn't work in Api 30(Android 11)


I am working on Bubbles in Android 11 and some functions doesn't work

I don't know how to fix this.

Android Studio writes:

Unresolved reference: setShortcutInfo

My NotificationCompat.Builder:

val builder = NotificationCompat.Builder(
            appContext,
            CHANNEL_WHATEVER
    )
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentTitle("Um, hi!")
            .setShortcutId("Settings")
            .setShortcutInfo(shortcutInfo)
            .setBubbleMetadata(bubble)

And my ShortcutInfoCompat.Builder:

val shortcutInfo = ShortcutInfoCompat.Builder(this, SHORTCUT_ID)
            .setLongLived(true)
            .setShortLabel("Settings")
            .setIntent(Intent(Settings.ACTION_SETTINGS))
            .setIcon(IconCompat.createWithResource(this, R.drawable.ic_launcher_foreground))
            .build()

ShortCutManagerCompat with pushdynamicshortcut returns:

Unresolved reference: pushDynamicShortcut

I Copy/Pasted code from this: Gitlab

Thanks.


Solution

  • I added mutableListOf for ShortcutInfo. So, working example:

    private var channelCreated = false
        private val notifId = 1337
        private val notifChannel = "Bubble Manager"
        private val shortcutId = "Bubble Manager"
        val bubble = showBubble()
    
        @RequiresApi(Build.VERSION_CODES.R)
        private fun buildBubbleNotification(appContext: Context): Notification {
            val pi = PendingIntent.getActivity(
                    appContext,
                    0,
                    Intent(appContext, BubbleActivity::class.java),
                    PendingIntent.FLAG_UPDATE_CURRENT
            )
            val bubble = NotificationCompat.BubbleMetadata.Builder()
                    .setDesiredHeight(4000)
                    .setIcon(IconCompat.createWithResource(appContext, R.drawable.ic_logo_bubble))
                    .setIntent(pi)
                    .apply { setAutoExpandBubble(true); setSuppressNotification(true) }
                    .build()
    
            ShortcutManagerCompat.addDynamicShortcuts(
                    context, mutableListOf(
                    ShortcutInfoCompat.Builder(context, shortcutId)
                            .setLongLived(true)
                            .setShortLabel("Bubble Manager")
                            .setIntent(Intent(Settings.ACTION_SETTINGS))
                            .setIcon(IconCompat.createWithResource(context, R.drawable.ic_logo_bubble))
                            .build()
            )
            )
    
    
            val builder = NotificationCompat.Builder(
                    appContext,
                    notifChannel
            )
                    .setSmallIcon(R.drawable.ic_logo_bubble)
                    .setContentTitle("Title")
                    .setShortcutId("Bubble Manager")
                    .setShortcutId(shortcutId)
                    .setBubbleMetadata(bubble)
    
            val person = Person.Builder()
                    .setBot(true)
                    .setName("A Bubble Bot")
                    .setImportant(true)
                    .build()
    
            val style = NotificationCompat.MessagingStyle(person)
                    .setConversationTitle("Bubble Manager")
    
            style.addMessage("It's Bubble Manager", System.currentTimeMillis(), person)
            builder.setStyle(style)
    
            return builder.build()
        }
    
        @RequiresApi(Build.VERSION_CODES.R)
        fun showBubble() {
            NotificationManagerCompat.from(context).let { mgr ->
                if (!channelCreated) {
                    mgr.createNotificationChannel(
                            NotificationChannel(
                                    notifChannel,
                                    "Whatever",
                                    NotificationManager.IMPORTANCE_DEFAULT
                            )
                    )
                    channelCreated = true
                }
                mgr.notify(notifId, buildBubbleNotification(context))
            }
        }