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

时间:2019-09-10 07:46:03

标签: 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);

///     显示通知的代码

public void createAlertNotification(上下文上下文,Intent resultIntent,String标题,String消息,位图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。

1 个答案:

答案 0 :(得分:0)

也许您需要权限验证