当前位置: 代码迷 >> java >> 如何在 Oreo 中以编程方式启用浮动通知和解锁屏幕通知?
  详细解决方案

如何在 Oreo 中以编程方式启用浮动通知和解锁屏幕通知?

热度:19   发布时间:2023-07-31 11:55:15.0

我想启用所有通知选项,但我不能这样做(请参阅 sreenshot)。 我安装了电报,默认情况下它的所有选项都是启用的。 为什么以及如何?

目前我有这个代码:

NotificationManager notifManager=(NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel mChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

    // Configure the notification channel.
    mChannel.setDescription("Common notifications");
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.RED);
    mChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
    mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
    mChannel.enableVibration(true);
    notifManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(icon)
                .setColor(color)
                .setTicker("")
                .setContentTitle("Title")
                .setContentText("ContextText")
                .setContentInfo("Info")
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setAutoCancel(true);
NotificationManager mNotificationManager =
        (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(SOMEID, mBuilder.build());

我将此权限添加到 Android 清单文件中。 它对我有用。

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />