android - 带有 RemoteView 的 Android 自定义通知未正确显示打击 Nougat OS
问题描述
我正在尝试使用创建自定义通知RemoteView
。它在 Nougat 及以上 OS 版本的设备中正确显示,但在 marshmallow 及以下 OS 版本的设备中存在问题。
这是默认的大图标(红色圆圈内的左侧)和默认时间(右侧红色圆圈内)在远程查看后不隐藏。
请建议我如何摆脱这个问题。
private void sendNotification() {
Long id = System.currentTimeMillis();
int notifyId = id.intValue();
RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.view_expanded_notification);
expandedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
expandedView.setTextViewText(R.id.notification_message, mEditText.getText());
// adding action to left button
Intent leftIntent = new Intent(this, NotificationIntentService.class);
leftIntent.setAction("left");
expandedView.setOnClickPendingIntent(R.id.left_button, PendingIntent.getService(this, 0, leftIntent, PendingIntent.FLAG_UPDATE_CURRENT));
// adding action to right button
Intent rightIntent = new Intent(this, NotificationIntentService.class);
rightIntent.setAction("right");
expandedView.setOnClickPendingIntent(R.id.right_button, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));
RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.view_collapsed_notification);
collapsedView.setTextViewText(R.id.timestamp, DateUtils.formatDateTime(this, System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "imp")
// these are the three things a NotificationCompat.Builder object requires at a minimum
.setSmallIcon(R.drawable.ic_pawprint)
.setContentTitle(NOTIFICATION_TITLE)
.setContentText(CONTENT_TEXT)
// notification will be dismissed when tapped
.setAutoCancel(true)
// tapping notification will open MainActivity
.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
// setting the custom collapsed and expanded views
.setCustomContentView(collapsedView)
.setCustomBigContentView(expandedView)
// setting style to DecoratedCustomViewStyle() is necessary for custom views to display
.setStyle(new NotificationCompat.DecoratedCustomViewStyle());
// retrieves android.app.NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("imp", "Test", importance);
channel.setDescription("For testing");
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(notifyId, builder.build());
} else {
NotificationManager notificationManager = (android.app.NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
}
这在 Nougat 及以上设备中正确显示。
解决方案
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, id);
builder.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(true)
.setCustomBigContentView(remoteExpandedViews)
.setCustomContentView(remoteCollapsedViews)
.setContentIntent(pendingIntent)
**.setPriority(Notification.PRIORITY_MAX)**
.setVibrate(new long[]{0});
在构建器中使用 .setPriority(Notification.PRIORITY_MAX) 将优先级设置为 PRIORITY_MAX
推荐阅读
- scala - 当我运行我的测试套件时,它们因 PSQLException 失败:致命:对不起,已经有太多客户了
- if-statement - 使用带有下拉列表的 Excel IF 语句
- angular - 角度组件未打开
- r - Ifelse NULL:获取数字而不是字符串值
- ios - 无法计算约束的确切值
- c++ - 如何在 C++ 中使用 Xpath 和 XQuery 解析 XML 文件?
- vba - 如何编写 Word VBA 语法来捕捉
- sql - 带有输出游标的 SQL 注入
- python - 如何判断来自 gensim 的 WikiCorpus 是否正常工作?
- pug - 我可以将 Pug mixin 的范围限制在其声明块中,以便无法通过扩展块来访问它吗?