NotificationCompat.Builder 中的 setShortcutInfo 和 ShortcutManagerCompat 中的 pushDynamicShortcut 在 Api 30(Android 11) 中不起作用

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

我正在 Android 11 中开发 Bubbles,但有些功能无法使用

我不知道如何解决这个问题。

Android工作室写道:

Unresolved reference: setShortcutInfo

我的NotificationCompat.Builder:

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

还有我的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

我Copy/Pasted代码来自: Gitlab

谢谢。

我为 ShortcutInfo 添加了 mutableListOf。所以,工作示例:

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))
        }
    }