App.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <script>
  2. export default {
  3. onLaunch: async function() {
  4. // #ifdef APP-PLUS
  5. this.getQuanxian(); // 查询是否开启通知
  6. // #endif
  7. },
  8. onShow: function() {},
  9. onHide: function() {},
  10. methods: {
  11. getQuanxian() {
  12. let platform = uni.getSystemInfoSync().platform; //首先判断app是安卓还是ios
  13. console.log(platform);
  14. if (platform == "ios") { //这里是ios的方法
  15. console.log("我是iOS");
  16. var UIApplication = plus.ios.import("UIApplication");
  17. var app = UIApplication.sharedApplication();
  18. var enabledTypes = 0;
  19. if (app.currentUserNotificationSettings) {
  20. var settings = app.currentUserNotificationSettings();
  21. enabledTypes = settings.plusGetAttribute("types");
  22. console.log("enabledTypes1:" + enabledTypes);
  23. if (enabledTypes == 0) { //如果enabledTypes = 0 就是通知权限没有开启
  24. uni.showModal({
  25. title: '提示',
  26. content: '请打开通知权限',
  27. success: res => {
  28. if (res.confirm) {
  29. this.openTongZhi()
  30. } else if (res.cancel) {
  31. console.log('用户点击取消');
  32. }
  33. }
  34. });
  35. } else {
  36. // uni.showToast({
  37. // title: "通知权限已开启",
  38. // icon: "none"
  39. // })
  40. }
  41. }
  42. plus.ios.deleteObject(settings);
  43. } else if (platform == "android") { //下面是安卓的方法
  44. // console.log("我是安卓", plus.android);
  45. var main = plus.android.runtimeMainActivity();
  46. var pkName = main.getPackageName();
  47. var uid = main.getApplicationInfo().plusGetAttribute("uid");
  48. var NotificationManagerCompat = plus.android.importClass(
  49. "android.support.v4.app.NotificationManagerCompat"
  50. );
  51. //android.support.v4升级为androidx
  52. if (NotificationManagerCompat == null) {
  53. NotificationManagerCompat = plus.android.importClass(
  54. "androidx.core.app.NotificationManagerCompat"
  55. );
  56. }
  57. var areNotificationsEnabled =
  58. NotificationManagerCompat.from(main).areNotificationsEnabled();
  59. // console.log(areNotificationsEnabled);
  60. // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
  61. if (!areNotificationsEnabled) {
  62. this.tongzhi = true; //这里也一样未开启权限,弹出弹窗
  63. }
  64. if (areNotificationsEnabled) {
  65. // uni.showToast({
  66. // title: "通知权限已开启",
  67. // icon: "none"
  68. // })
  69. } else {
  70. uni.showModal({
  71. title: '提示',
  72. content: '是否前往打开通知权限',
  73. success: res => {
  74. if (res.confirm) {
  75. this.openTongZhi()
  76. } else if (res.cancel) {
  77. console.log('用户点击取消');
  78. }
  79. }
  80. });
  81. }
  82. }
  83. },
  84. openTongZhi() { //弹窗按钮绑定方法
  85. let platform = uni.getSystemInfoSync().platform; //获取安卓还是ios
  86. if (platform == "ios") { //如果机型是ios,ios由于权限问题,可能需要手动开启
  87. var UIApplication = plus.ios.import("UIApplication");
  88. var app = UIApplication.sharedApplication();
  89. var settings = app.currentUserNotificationSettings();
  90. enabledTypes = settings.plusGetAttribute("types");
  91. var NSURL2 = plus.ios.import("NSURL");
  92. var setting2 = NSURL2.URLWithString("app-settings:");
  93. var application2 = UIApplication.sharedApplication();
  94. application2.openURL(setting2);
  95. plus.ios.deleteObject(setting2);
  96. plus.ios.deleteObject(NSURL2);
  97. plus.ios.deleteObject(application2);
  98. plus.ios.deleteObject(settings);
  99. } else if (platform == "android") { //如果机型是安卓
  100. var main = plus.android.runtimeMainActivity();
  101. var pkName = main.getPackageName();
  102. var uid = main.getApplicationInfo().plusGetAttribute("uid");
  103. var Intent = plus.android.importClass("android.content.Intent");
  104. var Build = plus.android.importClass("android.os.Build");
  105. //android 8.0引导
  106. if (Build.VERSION.SDK_INT >= 26) { //判断安卓系统版本
  107. var intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
  108. intent.putExtra("android.provider.extra.APP_PACKAGE", pkName);
  109. } else if (Build.VERSION.SDK_INT >= 21) { //判断安卓系统版本
  110. //android 5.0-7.0
  111. var intent = new Intent("android.settings.APP_NOTIFICATION_SETTINGS");
  112. intent.putExtra("app_package", pkName);
  113. intent.putExtra("app_uid", uid);
  114. } else {
  115. //(<21)其他--跳转到该应用管理的详情页
  116. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  117. var uri = Uri.fromParts(
  118. "package",
  119. mainActivity.getPackageName(),
  120. null
  121. );
  122. intent.setData(uri);
  123. }
  124. // 跳转到该应用的系统通知设置页
  125. main.startActivity(intent);
  126. }
  127. },
  128. }
  129. }
  130. </script>
  131. <style>
  132. /*每个页面公共css */
  133. </style>