How to open MIUI notifications settings?

1.8k Views Asked by At

I'm looking for a way to open in MIUI App Notification Settings screen directly from my application. Just like this: Settings -> Installed apps -> MY_APP -> Notifications.

How to construct intent for open this screen?

That's not the same like Notification Channels Manager added in Oreo - MIUI (Xiaomi) have their own notification manager.

4

There are 4 best solutions below

1
On
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);
1
On

the intents constructed to open notification settings are different on different sdk versions, you could try the code below, it runs well on my devices, assume you run this in the context of an activity:

    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);
1
On

This code works in 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);
1
On

Try this

To open App Notification Settings in android Oreo and higher (MIUI v9 and 10) you need to pass NOTIFICATION_CHANNEL_ID in intent

Here is the working code tested in MIUI v9 and the new version 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);
    }
}