NotificationCompat.Builder 中的 setShortcutInfo 和 ShortcutManagerCompat 中的 pushDynamicShortcut 在 API 3



我在Android 11中的气泡工作,一些功能不工作

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

Android Studio写:

Unresolved reference: setShortcutInfo

我NotificationCompat。建造者:

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

和我的快捷信息公司。建造者:

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

与pushdynamicshortcut比较:

Unresolved reference: pushDynamicShortcut

我复制/粘贴代码: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))
}
}

最新更新