Android Notification 相关 target 30

本文针对 Android target api 30。最低兼容 api 19

O以下

o以下没有消息渠道概念和相关的设置
1,进入设置通知权限页面

Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
    localIntent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
    localIntent.setAction(Intent.ACTION_VIEW);
    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
    localIntent.putExtra("com.android.settings.ApplicationPkgName", MainActivity.this.getPackageName());
}
startActivity(localIntent);

O以上

o以上必须设置渠道NotificationChannel
1,设置和设置渠道
NotificationChannel nc = new NotificationChannel(channelId,channelName,importance_level);

NotificationMannager.createNotificationChannel(nc);

2,对消息绑定渠道
Notification.Builder.setChannelId(channelId);

3,判断通知栏权限是否开启

   public static boolean isNotificationEnabled(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 = null;

        /* Context.APP_OPS_MANAGER */
        try {
            appOpsClass = Class.forName(AppOpsManager.class.getName());

            Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", 
                    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) {
            e.printStackTrace();
        }
        return false;
    }

4,跳转到通知权限设置页面

Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
    localIntent.setData(Uri.fromParts("package", MainActivity.this.getPackageName(), null));
} else if (Build.VERSION.SDK_INT <= 8) {
    localIntent.setAction(Intent.ACTION_VIEW);
    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
    localIntent.putExtra("com.android.settings.ApplicationPkgName", MainActivity.this.getPackageName());
}
startActivity(localIntent);

Q以上

Android Notification 相关 target 30

上一篇:hadoop学习(三)hadoop集群从windows移植到linux中问题与技巧


下一篇:js获取鼠标点击的对象,点击另一个按钮删除该对象