[英]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。
YES
From android N , this field may be null
YES
从android N ,此字段可能为null
The notification view is determined by the inputs to Notification.Builder;
通知视图由
Notification.Builder;
的输入确定Notification.Builder;
a custom RemoteViews
can optionally be supplied with Notification.Builder.setCustomContentView(RemoteViews)
可以选择提供自定义
RemoteViews
Notification.Builder.setCustomContentView(RemoteViews)
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.