NotificationUtil.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package org.cocos2dx.javascript.util;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.app.AppOpsManager;
  5. import android.app.NotificationManager;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.pm.ApplicationInfo;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.support.annotation.RequiresApi;
  13. import android.support.v4.app.NotificationManagerCompat;
  14. import java.lang.reflect.Field;
  15. import java.lang.reflect.InvocationTargetException;
  16. import java.lang.reflect.Method;
  17. public class NotificationUtil {
  18. public static void choseNotification(Activity activity) {
  19. //如果是新应用,查询权限并且询问
  20. if (SPutil.getPrefInt(activity, SPutil.notificationType, 0) == 0) {
  21. if (org.cocos2dx.javascript.util.NotificationUtil.areNotificationsEnabled(activity)) {
  22. //有权限
  23. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_OPEN);
  24. } else {
  25. //没权限
  26. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_CLOSE);
  27. org.cocos2dx.javascript.util.NotificationUtil.showDialog(activity);
  28. }
  29. } else {
  30. //不是第一次新应用,如果设置了有权限,就设置系统默认的权限,不提示
  31. if (org.cocos2dx.javascript.util.NotificationUtil.areNotificationsEnabled(activity)) {
  32. //有权限
  33. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_OPEN);
  34. } else {
  35. //没权限
  36. SPutil.setPrefInt(activity, SPutil.notificationType, SPutil.NOTIFICATION_CLOSE);
  37. }
  38. }
  39. }
  40. private static boolean areNotificationsEnabled(Context context) {
  41. NotificationManagerCompat.from(context).areNotificationsEnabled();
  42. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  43. return true;
  44. }
  45. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  46. return isEnableV19(context);
  47. } else {
  48. return isEnableV26(context);
  49. }
  50. }
  51. @RequiresApi(api = Build.VERSION_CODES.KITKAT)
  52. private static boolean isEnableV19(Context context) {
  53. final String CHECK_OP_NO_THROW = "checkOpNoThrow";
  54. final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  55. AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
  56. ApplicationInfo appInfo = context.getApplicationInfo();
  57. String pkg = context.getApplicationContext().getPackageName();
  58. int uid = appInfo.uid;
  59. Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
  60. try {
  61. appOpsClass = Class.forName(AppOpsManager.class.getName());
  62. Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
  63. Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
  64. int value = (int) opPostNotificationValue.get(Integer.class);
  65. return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
  66. } catch (ClassNotFoundException e) {
  67. } catch (NoSuchMethodException e) {
  68. } catch (NoSuchFieldException e) {
  69. } catch (InvocationTargetException e) {
  70. } catch (IllegalAccessException e) {
  71. } catch (Exception e) {
  72. }
  73. return false;
  74. }
  75. private static boolean isEnableV26(Context context) {
  76. ApplicationInfo appInfo = context.getApplicationInfo();
  77. String pkg = context.getApplicationContext().getPackageName();
  78. int uid = appInfo.uid;
  79. try {
  80. NotificationManager notificationManager = (NotificationManager)
  81. context.getSystemService(Context.NOTIFICATION_SERVICE);
  82. Method sServiceField = notificationManager.getClass().getDeclaredMethod("getService");
  83. sServiceField.setAccessible(true);
  84. Object sService = sServiceField.invoke(notificationManager);
  85. Method method = sService.getClass().getDeclaredMethod("areNotificationsEnabledForPackage"
  86. , String.class, Integer.TYPE);
  87. method.setAccessible(true);
  88. return (boolean) method.invoke(sService, pkg, uid);
  89. } catch (Exception e) {
  90. return true;
  91. }
  92. }
  93. private static void showDialog(final Activity act) {
  94. AlertDialog.Builder builder = new AlertDialog.Builder(act)
  95. // .setIcon(R.mipmap.ic_launcher)
  96. .setTitle("提示")
  97. .setMessage("检测到您没有打开通知权限,是否去打开")
  98. .setPositiveButton("确定", new DialogInterface.OnClickListener() {
  99. @Override
  100. public void onClick(DialogInterface dialogInterface, int i) {
  101. dialogInterface.cancel();
  102. Intent localIntent = new Intent();
  103. localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  104. if (Build.VERSION.SDK_INT >= 9) {
  105. localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  106. localIntent.setData(Uri.fromParts("package", act.getPackageName(), null));
  107. } else if (Build.VERSION.SDK_INT <= 8) {
  108. localIntent.setAction(Intent.ACTION_VIEW);
  109. localIntent.setClassName("com.android.settings",
  110. "com.android.settings.InstalledAppDetails");
  111. localIntent.putExtra("com.android.settings.ApplicationPkgName",
  112. act.getPackageName());
  113. }
  114. act.startActivity(localIntent);
  115. }
  116. }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
  117. @Override
  118. public void onClick(DialogInterface dialog, int i) {
  119. dialog.cancel();
  120. }
  121. });
  122. builder.create().show();
  123. }
  124. }