答案 0 :(得分:89)
以下内容适用于Android 5.0(Lollipop)及以上版本:
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 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
注意:这在Android 5-7中不受官方支持,但它可以正常工作。从Android 8开始,它正式受到支持。此代码不向后兼容5.0之前的Android版本。
答案 1 :(得分:42)
我合并了Sergei和Shhp的解决方案以支持所有案例:
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
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("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
} else {
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + context.getPackageName()));
}
context.startActivity(intent);
答案 2 :(得分:9)
我已为Android 8.0 Oreo API 26或更高版本添加了频道通知设置。 Android 4.4,KitKat提供了一个解决方案。
频道通知设置的用法:
// PRIMARY_CHANNEL:
goToNotificationSettings(getString(R.string.PRIMARY_CHANNEL), mContext);
// SECONDARY_CHANNEL:
goToNotificationSettings(getString(R.string.SECONDARY_CHANNEL), mContext);
应用程序通知设置的用法:
goToNotificationSettings(null, mContext);
goToNotificationSettings的方法:
public void goToNotificationSettings(String channel, Context context) {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
intent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
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.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);
}
答案 3 :(得分:5)
我使用此代码(kitkat和下一版本):
git checkout SHA
答案 4 :(得分:1)
使用ACTION_APP_NOTIFICATION_SETTINGS
将列出应用程序的所有频道:
Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
startActivity(intent);
要打开单个频道的设置,可以使用ACTION_CHANNEL_NOTIFICATION_SETTINGS
:
您可以在其中更改各个频道的sound,vibration.etc
设置。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent("android.settings.CHANNEL_NOTIFICATION_SETTINGS");
intent.putExtra("android.provider.extra.CHANNEL_ID", "ChannelID");
intent.putExtra("android.provider.extra.APP_PACKAGE", getPackageName());
startActivity(intent);
}
答案 5 :(得分:1)
我合并了上面一些答案的代码,并进行了少许编辑,我已经测试过,它在Android KitKat,Lollipop,棉花糖,牛轧糖,Oreo和Pie,API级别19-28上都能正常工作
public void goToNotificationSettings(Context context) {
String packageName = context.getPackageName();
try {
Intent intent = new Intent();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra("android.provider.extra.APP_PACKAGE", packageName);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", packageName);
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:" + packageName));
} else {
return;
}
startActivity(intent);
} catch (Exception e) {
// log goes here
}
}
答案 6 :(得分:1)
我想提供@Helix答案的纯代码版本:
fun openNotificationsSettings() {
val intent = Intent()
when {
Build.VERSION.SDK_INT > Build.VERSION_CODES.O -> intent.setOpenSettingsForApiLarger25()
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> intent.setOpenSettingsForApiBetween21And25()
else -> intent.setOpenSettingsForApiLess21()
}
app.startActivity(intent)
}
private fun Intent.setOpenSettingsForApiLarger25(){
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra("android.provider.extra.APP_PACKAGE", app.packageName)
}
private fun Intent.setOpenSettingsForApiBetween21And25(){
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
putExtra("app_package", app.packageName)
putExtra("app_uid", app.applicationInfo?.uid)
}
private fun Intent.setOpenSettingsForApiLess21(){
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
addCategory(Intent.CATEGORY_DEFAULT)
data = Uri.parse("package:" + app.packageName)
}
当分支到紧凑类时,甚至可以走得更远。并创建一个when
所在的工厂。
答案 7 :(得分:0)
import UIKit
import UserNotifications
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//For FireBase Configs
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
FirebaseApp.configure()
return true
}
func application(_ application: UIApplication,open url: URL,sourceApplication: String?, annotation: Any) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!,sourceApplication: sourceApplication,annotation: annotation)
}
//FireBase Notifications Begins
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//Play alert sound
utilityHelper.playAudio(fileName: "udio", fileExtenstion: "mp3")
let json = JSON(userInfo)
//show notification
completionHandler(UIBackgroundFetchResult.newData)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
NSLog("show notification")
completionHandler(UNNotificationPresentationOptions.alert)
}
func application(received remoteMessage: MessagingRemoteMessage) {
}
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken as Data
if let regToken = InstanceID.instanceID().token() {
}
}
//FireBase Notifications End
}
答案 8 :(得分:0)
最后,我测试了几乎所有设备并正常工作。给出的代码如下
public void goToPushSettingPage(Context context) {
try {
Intent intent=new Intent();
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(ConstUtil.PUSH_SETTING_APP_PACKAGE,context.getPackageName());
intent.putExtra(ConstUtil.PUSH_SETTING_APP_UID,context.getApplicationInfo().uid);
}else{
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse(ConstUtil.PUSH_SETTING_URI_PACKAGE+context.getPackageName()));
}
startActivity(intent);
} catch (Exception e) {
// log goes here
}
}
答案 9 :(得分:0)
对于懒惰的人,这是@Helix答案的kotlin版本:
fun openAppNotificationSettings(context: Context) {
val intent = Intent().apply {
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
action = "android.settings.APP_NOTIFICATION_SETTINGS"
putExtra("app_package", context.packageName)
putExtra("app_uid", context.applicationInfo.uid)
}
else -> {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
addCategory(Intent.CATEGORY_DEFAULT)
data = Uri.parse("package:" + context.packageName)
}
}
}
context.startActivity(intent)
}