public class NotificationUtil {
private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
public static void startNotificationSetting(Context context) {
try {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
localIntent.putExtra(Settings.EXTRA_CHANNEL_ID, context.getApplicationInfo().uid);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
localIntent.putExtra("app_package", context.getPackageName());
localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
}
context.startActivity(localIntent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", context.getPackageName(), null));
context.startActivity(intent);
} catch (Exception e1) {
}
}
}
public static boolean isNotificationEnabled(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return isEnableV26(context);
} else {
return isEnableV19(context);
}
}
/**
* Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT 19及以上
*/
public static boolean isEnableV19(Context context) {
AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass;
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
int value = (Integer) opPostNotificationValue.get(Integer.class);
return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (Exception e) {
}
return false;
}
/**
* Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 针对8.0及以上设备
*/
public static boolean isEnableV26(Context context) {
try {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return false;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return notificationManager.areNotificationsEnabled();
}
Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
sServiceField.setAccessible(true);
Object sService = sServiceField.invoke(notificationManager);
ApplicationInfo appInfo = context.getApplicationInfo();
String pkg = context.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage", String.class, Integer.TYPE);
method.setAccessible(true);
return (boolean) method.invoke(sService, pkg, uid);
} catch (Exception e) {
}
return false;
}
public static void cancelAllNotifications() {
if (CCApplication.getInstance() == null) {
return;
}
NotificationManager notificationManager = (NotificationManager) CCApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager == null) {
return;
}
try {
notificationManager.cancelAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}
安卓Android最新版本检测消息推送push notification是否可用,如果Push权限没有打开,跳转到setting让用户去打开push 权限
最新推荐文章于 2025-07-30 20:36:15 发布
这个代码片段展示了如何在Android中启动应用的通知设置界面,并检查通知权限是否开启。对于API 26以上,它使用`NotificationManager`来判断,而对于更低版本,它利用`AppOpsManager`进行检查。同时,提供了取消所有通知的功能。
850

被折叠的 条评论
为什么被折叠?



