0

我正在使用 Android 11 中的 Bubbles,但某些功能不起作用

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

Android Studio 写道:

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 与 pushdynamicshortcut 返回:

Unresolved reference: pushDynamicShortcut

我从这里复制/粘贴代码: Gitlab

谢谢。

4

1 回答 1

0

我为 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))
        }
    }
于 2021-03-21T14:49:52.213 回答