NotificationUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.jyc.threegames.utils;
  2. import android.app.AppOpsManager;
  3. import android.app.Notification;
  4. import android.app.NotificationChannel;
  5. import android.app.NotificationManager;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.pm.ApplicationInfo;
  9. import android.graphics.BitmapFactory;
  10. import android.graphics.Color;
  11. import android.net.Uri;
  12. import android.os.Build;
  13. import android.provider.Settings;
  14. import androidx.core.app.NotificationCompat;
  15. import com.jyc.threegames.R;
  16. import java.lang.reflect.Field;
  17. import java.lang.reflect.Method;
  18. import static android.content.Context.NOTIFICATION_SERVICE;
  19. public class NotificationUtils {
  20. public static Notification GET_NOTIFICATION(Context context, String tittle, String body){
  21. if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  22. NotificationChannel channel =new NotificationChannel("three_game_important",
  23. "three_game", NotificationManager.IMPORTANCE_HIGH);
  24. // channel.enableLights(true);
  25. // channel.setLightColor(Color.RED);
  26. // channel.setShowBadge(true);
  27. ((NotificationManager)context.getSystemService(NOTIFICATION_SERVICE)).createNotificationChannel(channel);
  28. }
  29. NotificationCompat.Builder build = new NotificationCompat.Builder(context, "three_game_important");
  30. build.setContentTitle(tittle);
  31. build.setContentText(body);
  32. build.setTicker(tittle);
  33. // if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
  34. // build.setPriority(NotificationManager.IMPORTANCE_MAX);
  35. // else
  36. // build.setPriority(Notification.PRIORITY_HIGH);
  37. // build.setAutoCancel(true);
  38. // build.setWhen(System.currentTimeMillis());
  39. // build.setDefaults(Notification.DEFAULT_ALL);
  40. build.setSmallIcon(R.mipmap.ic_launcher);
  41. // build.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));
  42. return build.build();
  43. }
  44. private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
  45. private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
  46. public static boolean IS_NOTIFICATION_ENABLE(Context context) {
  47. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  48. ///< 8.0手机以上
  49. if (((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).getImportance() == NotificationManager.IMPORTANCE_NONE) {
  50. return false;
  51. }
  52. }
  53. AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
  54. ApplicationInfo appInfo = context.getApplicationInfo();
  55. String pkg = context.getApplicationContext().getPackageName();
  56. int uid = appInfo.uid;
  57. Class appOpsClass = null;
  58. try {
  59. appOpsClass = Class.forName(AppOpsManager.class.getName());
  60. Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
  61. String.class);
  62. Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
  63. int value = (Integer) opPostNotificationValue.get(Integer.class);
  64. return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. return false;
  69. }
  70. public static void REQUEST_NOTIFY(Context context) {
  71. /**
  72. * 跳到通知栏设置界面
  73. * @param context
  74. */
  75. Intent localIntent = new Intent();
  76. ///< 直接跳转到应用通知设置的代码
  77. if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  78. localIntent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
  79. localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
  80. }
  81. else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
  82. android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  83. localIntent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
  84. localIntent.putExtra("app_package", context.getPackageName());
  85. localIntent.putExtra("app_uid", context.getApplicationInfo().uid);
  86. } else if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
  87. localIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  88. localIntent.addCategory(Intent.CATEGORY_DEFAULT);
  89. localIntent.setData(Uri.parse("package:" + context.getPackageName()));
  90. } else {
  91. ///< 4.4以下没有从app跳转到应用通知设置页面的Action,可考虑跳转到应用详情页面,
  92. localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  93. if (Build.VERSION.SDK_INT >= 9) {
  94. localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  95. localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
  96. } else if (Build.VERSION.SDK_INT <= 8) {
  97. localIntent.setAction(Intent.ACTION_VIEW);
  98. localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
  99. localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
  100. }
  101. }
  102. context.startActivity(localIntent);
  103. }
  104. }