首页 > 解决方案 > 带有 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 及以上设备中正确显示。

在此处输入图像描述

标签: androidpush-notificationandroid-6.0-marshmallowremoteview

解决方案


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


推荐阅读