简体   繁体   English

如何在Android Nougat及以上版本中创建Notification.contentView?

[英]How to create Notification.contentView in android Nougat and above?

I use Notification.contentView to replicate notification views: 我使用Notification.contentView来复制通知视图:

View notificationView = notification.contentView.apply(context, parent);

Unfortunately as of version N, Notification.contentView may be null and deprecated, so how can I create Notification.contentView manually? 不幸的是,从版本N开始, Notification.contentView可能为null并且已弃用,因此如何手动创建Notification.contentView

Generally I create notification this way: 通常我会这样创建通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
        .setColor(ContextCompat.getColor(this, R.color.colorAccent))
        .setContentTitle(title)
        .setContentText(text)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(when)
        .setSmallIcon(smallIcon);

Then if I create the contentView manually, what can I do to map all the above settings? 然后,如果我手动创建contentView,我该怎么做才能映射上述所有设置?
Important note: I don't call setCustomContentView, I want to reproduce a contentView for the standard notification. 重要说明:我不调用setCustomContentView,我想为标准通知重现contentView。

Notification.contentView()

SAMPLE CODE 示例代码

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
                NotificationManager.IMPORTANCE_HIGH);

        notificationChannel.setDescription("");
        notificationChannel.enableLights(true);
        notificationChannel.enableVibration(true);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.custom_layout);

    notificationBuilder.setAutoCancel(true)
            .setColor(ContextCompat.getColor(this, R.color.colorAccent))
            .setContentTitle(getString(R.string.app_name))
            .setContentText("")
            .setDefaults(Notification.DEFAULT_ALL)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setCustomContentView(notificationView) // set here your RemoteViews
            .setAutoCancel(true);

OUTPUT OUTPUT

在此输入图像描述

Answer my own question: 回答我自己的问题:
Create contentView by Notification.Builder : 通过Notification.Builder创建contentView

builder.createContentView();

Create contentView by Notification : Notification创建contentView

Notification.Builder.recoverBuilder(context, notification).createContentView();

Since Notification.Builder.createContentView() was introduced in api level 24, so the above code can only be called from Nougat 7.0 or newer devices; 由于Notification.Builder.createContentView()是在api级别24中引入的,因此上述代码只能从Nougat 7.0或更新的设备调用; For a lower version phones, it's always safe to reference non-null Notification.contentView directly, it's created automatically by the android system after builder.build() was called. 对于较低版本的手机,直接引用非null Notification.contentView总是安全的,它是在调用builder.build()之后由android系统自动创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问Android 7.0或更高版本的Nougat中的进程? - How to access to the processes in Android 7.0 or above Nougat? 如何以编程方式在Android Nougat,Oreo及更高版本中阻止通话? - How to Block Call in Android Nougat, Oreo and above programatically? Android 7中的通知contentView和bigContentView为空 - Notification contentView and bigContentView are null in Android 7 如何从URL映像牛轧糖及更高版本的OS版本设置自定义通知图标? - How to set custom notification icons from URL image Nougat and above OS Version? 如何在Android Nougat中管理Notification的setNumber或setSubText方法 - How to manage setNumber or setSubText method of Notification in Android Nougat android以编程方式在牛轧糖上方关闭屏幕 - android programmatically screen off above Nougat Android Nougat中的通知计数(SDK 24) - Notification count in Android Nougat (SDK 24) Android牛轧糖中的通知中未显示图标 - Icon is not getting displayed in notification in Android nougat Android Nougat通知栏中的彩色文本和图标 - Colored text and icons in notification bar in Android Nougat 如何在Android版本7.0(牛轧糖)及以上版本中将InputType设置为InputType.TYPE_CLASS_NUMBER - how to set InputType to InputType.TYPE_CLASS_NUMBER in Android Version 7.0 (nougat) and above
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM