Android 12上未显示自定义通知

3

我需要翻译的内容:

我的通知代码有问题,在我的安卓9设备上运行良好,但在安卓12上不起作用。我注意到可能与RemoteViews有关,但我不确定。

这是我的代码:

object NotificationUtils {

private fun createNotification(context: Context, activity: Activity, layout: Int, channelId: String ): Notification {


    return NotificationCompat.Builder(context, channelId)
        .setCustomContentView(RemoteViews(activity.packageName, layout))
        .setSmallIcon(R.drawable.ic_logo_alfred_sin_texto)
        .setColor(ContextCompat.getColor(context, R.color.greenCrusoe))
        .setAutoCancel(true)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .build()
}


fun executeNotification(
    context: Context, notificationId: Int, activity: Activity, layout: Int, channelId: String) {
    val notificationManager = NotificationManagerCompat.from(context)
    notificationManager
        .notify(notificationId, createNotification(context, activity, layout, channelId))
}


fun createNotificationChannel(channelId : String, channelName: String, activity: Activity){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        val channel = NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_HIGH)
            .apply {
                lightColor = Color.RED
                enableLights(true)
            }
        val manager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.createNotificationChannel(channel)
    }
}

首先我调用createNotificationChannel()函数,然后执行executeNotification,该函数调用createNotification函数。

这是我传递给createNotification函数的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/greenCrusoe"
app:cardCornerRadius="@dimen/common_max_margin_padding_value">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_icon_checkcheck_white"
    android:layout_gravity="center"
    android:layout_marginTop="@dimen/common_margin_value" />

<TextView
    android:id="@+id/tv_successfully_notification"
    style="@style/textViewTitleStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingVertical="@dimen/common_min_margin_value"
    android:layout_gravity="center"
    android:drawablePadding="@dimen/common_min_margin_value"
    android:text="@string/not_changed_pin_success_text"
    android:textAlignment="center"
    android:textColor="@color/white"
    android:textSize="15sp" />

你找到解决方案了吗? - khushbu vadi
2个回答

2

虽然有一些应用程序仍然能够做到这一点。Android 13,它们仍然显示完全自定义的通知。 https://postimg.cc/nCFnz4h9 我想知道他们是如何做到的? - Cristian

2
尝试使用:
setStyle(NotificationCompat.DecoratedCustomViewStyle())

它对我有效。

val builder = NotificationCompat.Builder(this, MyVoiceRecorder.CHANNEL_ID) .setSmallIcon(R.drawable.ic_launcher_foreground) .setStyle(NotificationCompat.DecoratedCustomViewStyle()) .setCustomContentView(remoteView) .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE) .build() - Asfir Ali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接