java - 有什么方法可以以编程方式在MIUI 10上启用锁定屏幕通知吗?

标签 java android

我正在创建一个消息传递应用程序,因此通知在我的应用程序中起着重要的作用。我已经创建了通知显示处理程序,通知在一台,Motorola,Samsung上正常运行,但在Mi设备上却没有。默认情况下,我的应用程序在MI设备(MIUI10)中禁用锁屏通知。但是,当我检查了诸如WhatsApp,Snapchat之类的流行应用程序的设置时,将启用所有选项,例如锁屏通知,声音,浮动通知。

我可以手动启用所有设置,但我想以编程方式进行设置,因此用户不需要这样做。
我尝试使用其方法setLockscreenVisibility()使用通知通道(适用于上述O设备),但此方法不起作用。

notificationChannel = new NotificationChannel(ChatSDKMessageChannel, name, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(soundUri, audioAttributes);
channel.enableVibration(true);


///
    显示通知的代码

公共无效createAlertNotification(上下文上下文,意图结果意图,字符串标题,字符串消息,位图largeIcon,int smallIconResID,Uri soundUri,int编号){

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    Notification.Builder builder =
            new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setSmallIcon(smallIconResID)
                    .setVibrate(new long[]{0, 250, 100, 250})
                    .setSound(soundUri)
                    .setNumber(number)
                    .setContentIntent(pendingIntent)
                    .setTicker(title + ": " + message)
                    .setPriority(Notification.PRIORITY_HIGH);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (largeIcon != null) {
        builder.setLargeIcon(ImageUtils.scaleImage(largeIcon, (int) (context.getResources().getDisplayMetrics().density * 48)));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setColor(ChatSDK.config().pushNotificationColor);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        builder.setChannelId(ChatSDKMessageChannel);

        CharSequence name = context.getString(R.string.app_name);
        String description = context.getString(R.string.push_channel_name);

        NotificationChannel channel = new NotificationChannel(ChatSDKMessageChannel, name, NotificationManager.IMPORTANCE_HIGH);
        channel.enableVibration(true);
        channel.setDescription(description);

        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }

    }


    Notification notification = builder.build();

    notification.flags = Notification.FLAG_AUTO_CANCEL ;

    notificationManager.notify(MESSAGE_NOTIFICATION_ID, notification);

    wakeScreen(context);
}


screenshot of notification setting of my demo app

screenshot of WhatsApp

我需要为我的应用程序启用所有通知选项,就像WhatsApp和snapchat。

最佳答案

也许您需要权限验证

关于java - 有什么方法可以以编程方式在MIUI 10上启用锁定屏幕通知吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57866224/

相关文章:

java - 如何以编程方式更改样式属性值?

java - 尽管在 Java 中被声明为基类型,如何限制同一对象之间的操作

java - FileInputStream读取方法的风格

android - NPE 同时向 AChartEngine 折线图添加数据

android - EditText、inputType 值 (XML)

android - 在 ImageView 上放置 TextView

java - 在Java中表示可变长度矩阵(二维数组)的最佳方式?

java - 高质量语言;整数列表作为输入查询

java - 注入(inject)与继承

android - 由 java.lang.IllegalStateException : Can't create ViewModelProvider for detached fragment 引起