Как открыть настройки уведомлений MIUI? - PullRequest
0 голосов
/ 17 сентября 2018

Я ищу способ открыть в MIUI Настройки уведомлений приложения экран прямо из моего приложения.Вот так: Настройки -> Установленные приложения -> MY_APP -> Уведомления.

Как создать намерение открыть этот экран?

Это не то же самое, что каналы уведомленийМенеджер добавлен в Oreo - MIUI (Xiaomi) имеет собственный менеджер уведомлений.

Ответы [ 4 ]

0 голосов
/ 29 ноября 2018

Попробуйте это

Чтобы открыть Настройки уведомлений приложения в Android Oreo и выше (MIUI v9 и 10), вам нужно передать NOTIFICATION_CHANNEL_ID в intent

Вот рабочий код, протестированный в MIUI v9 и новой версии 10

SAMPLE CODE

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import java.util.Objects;


public class MainActivity extends AppCompatActivity {

    String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        long pattern[] = {0, 1000, 500, 1000};

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);
            Objects.requireNonNull(mNotificationManager).createNotificationChannel(notificationChannel);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
                channel.canBypassDnd();
            }

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

            notificationBuilder.setAutoCancel(true)
                    .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                    .setContentTitle(getString(R.string.app_name))
                    .setContentText("Test")
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setAutoCancel(true);


            mNotificationManager.notify(1000, notificationBuilder.build());
        }

    }

    public void ClickMe(View view) {

        notificationSettings(NOTIFICATION_CHANNEL_ID,this);
    }


    public void notificationSettings(String channel, Context context) {

        Intent intent = new Intent();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (channel != null) {
                intent.setAction(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
                intent.putExtra(Settings.EXTRA_CHANNEL_ID, channel);
            } else {
                intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            }
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());

        } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
        } else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
            intent.putExtra("app_package", context.getPackageName());
            intent.putExtra("app_uid", context.getApplicationInfo().uid);
        } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
            intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setData(Uri.parse("package:" + context.getPackageName()));
        }
        context.startActivity(intent);
    }
}
0 голосов
/ 27 ноября 2018
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");

//for Android 5-7
intent.putExtra("app_package", getPackageName());
intent.putExtra("app_uid", getApplicationInfo().uid);

// for Android O
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());

startActivity(intent);
0 голосов
/ 27 ноября 2018

намерения, созданные для открытия настроек уведомлений, отличаются в разных версиях SDK, вы можете попробовать приведенный ниже код, он хорошо работает на моих устройствах, предположим, что вы запускаете его в контексте действия:

    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //above android 8.0 jump to notification channels
    if (Build.VERSION.SDK_INT >= 26) {
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", "your.package.name", null));
    }
    //android 5.0-7.0 notification settings
    if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 26) {
        intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
        intent.putExtra("app_package", "your.package.name");
        intent.putExtra("app_uid", getApplicationInfo().uid);
    }
    //others
    if (Build.VERSION.SDK_INT < 21) {
        intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
        intent.setData(Uri.fromParts("package", "your.package.name", null));
    }
    startActivity(intent);
0 голосов
/ 04 октября 2018

Этот код работает в MIUI 10:

Intent settingsIntent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
                              .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                              .putExtra(Settings.EXTRA_APP_PACKAGE, "YourPackage");
                      startActivity(settingsIntent);
...