我正在寻找一种直接从我的应用程序在 MIUI 应用程序通知设置屏幕中打开的方法。就像这样:设置 -> 已安装的应用程序 -> MY_APP -> 通知。
如何构建打开此屏幕的意图?
这与奥利奥中添加的通知通道管理器不同 - MIUI(小米)有自己的通知管理器。
我正在寻找一种直接从我的应用程序在 MIUI 应用程序通知设置屏幕中打开的方法。就像这样:设置 -> 已安装的应用程序 -> MY_APP -> 通知。
如何构建打开此屏幕的意图?
这与奥利奥中添加的通知通道管理器不同 - MIUI(小米)有自己的通知管理器。
构建打开通知设置的意图在不同的 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);
尝试这个
NOTIFICATION_CHANNEL_ID
要在 android Oreo 及更高版本(MIUI v9 和 10)中打开应用通知设置,您需要传入intent
这是在 MIUI v9 和新版本 10 中测试的工作代码
示例代码
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);
}
}
此代码适用于 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);
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);