MIUI悬浮通知无法正常工作

4

我正在努力让我的通知显示为heads up或者peek,也称为悬浮通知。

在原生Android和Lineage OS API 21-26上工作正常。

如何才能在MIUI 8中使其正常工作?

该应用程序已允许所有通知权限。

这是我的通知代码示例:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
    NotificationCompat.Builder notification = new NotificationCompat.Builder(context, "service_notifications")
        .setContentTitle(name)
        .setContentText("\uD83D\uDD14" + res.getText(R.string.imminent_arrival_text) + String.valueOf(Long.parseLong(eta) / 60) + res.getText(R.string.minutes_at_text) + simpleETA)
        .setSmallIcon(R.drawable.ic_stat_name)
        .setContentIntent(null)
        .addAction(R.drawable.ic_dialog_close_light, res.getText(R.string.stop_following_label), closePendingIntent)                                
        .setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.quite_impressed))
        .setVibrate(new long[]{1000, 1000})                                    
        .setPriority(NotificationCompat.PRIORITY_MAX)                                    
        .setCategory(NotificationCompat.CATEGORY_MESSAGE);

    notificationManager.notify(
        Constants.NOTIFICATION_ID.FOREGROUND_SERVICE,
        notification.build());
}

你尝试添加震动模式了吗? - HedeH
是的,我有。我已经添加了上面代码的一个示例。该代码在原生Android和LineageOS上生成弹出窗口,但在MIUI上没有...是的,所有通知权限已经授予该应用程序。 - eben80
尝试添加setDefaults(Notification.DEFAULT_ALL)。 - HedeH
我已经添加了MIUi的解决方案,请检查。 - Santanu Sur
1个回答

4
你可以有两种方式实现此操作。
方法一: 1. 进入“设置”。 2. 找到“已安装应用程序”并迁移到你的应用程序。 3. 选择你的应用程序并进入通知设置(打开“优先级”和“浮动通知”)。
方法二: 创建一个远程视图(在“res/layout”中创建自己的布局),并设置“customHeadsUpNotification”。
 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notif_custom_view);

 notification_builder.createHeadsUpContentView(remoteViews);

不要忘记在进行这些更改后修改notification_id。

1
谢谢!我第一次能够在MIUI上触发悬浮通知了。现在我会看看是否有意义重建所有的通知,使用RemoveViews代替。 - eben80
createHeadsUpContentView(remoteViews)不接受remoteViews作为参数。而且在MIUI 10上对我也不起作用。你能帮忙吗,@santanu-sur? - Shahid Kamal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接