自定义通知在Android 12上不显示

zpqajqem  于 2023-06-20  发布在  Android
关注(0)|答案(2)|浏览(336)

我有一个问题,我的通知代码,在我的设备与Android 9它工作得很好,但不是在Android 12.我注意到,也许这是一些有关的远程视图,但我不确定.
这是我的代码:
public object {

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" />
wfsdck30

wfsdck301#

https://developer.android.com/training/notify-user/custom-notification#create_a_fully_custom_notification_layout
如链接所示:
注意:从Android 12(API级别31)开始,针对Android 12或更高版本的应用不再能够创建完全自定义的通知。相反,系统会应用一个与Notification.DecoratedCustomViewStyle的行为几乎相同的标准模板。

sqxo8psd

sqxo8psd2#

尝试使用:

setStyle(NotificationCompat.DecoratedCustomViewStyle())

对我很有效

相关问题