当前位置:   article > 正文

uniapp打开手机通知权限设置_uniapp拒绝消息通知设置

uniapp拒绝消息通知设置
  1. /**
  2. * 设置手机通知权限
  3. */
  4. function setPermissions() {
  5. // #ifdef APP-PLUS
  6. if (plus.os.name == 'Android') { // 判断是Android
  7. var main = plus.android.runtimeMainActivity();
  8. var pkName = main.getPackageName();
  9. var uid = main.getApplicationInfo().plusGetAttribute("uid");
  10. var NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
  11. //android.support.v4升级为androidx
  12. if (NotificationManagerCompat == null) {
  13. NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
  14. }
  15. var areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
  16. // 未开通‘允许通知’权限,则弹窗提醒开通,并点击确认后,跳转到系统设置页面进行设置
  17. if (!areNotificationsEnabled) {
  18. uni.showModal({
  19. title: '通知权限开启提醒',
  20. content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
  21. showCancel: false,
  22. confirmText: '去设置',
  23. success: function(res) {
  24. if (res.confirm) {
  25. var Intent = plus.android.importClass('android.content.Intent');
  26. var Build = plus.android.importClass("android.os.Build");
  27. //android 8.0引导
  28. if (Build.VERSION.SDK_INT >= 26) {
  29. var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
  30. intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
  31. } else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
  32. var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
  33. intent.putExtra("app_package", pkName);
  34. intent.putExtra("app_uid", uid);
  35. } else { //(<21)其他--跳转到该应用管理的详情页
  36. intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
  37. var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
  38. intent.setData(uri);
  39. }
  40. // 跳转到该应用的系统通知设置页
  41. main.startActivity(intent);
  42. }
  43. }
  44. });
  45. }
  46. } else if (plus.os.name == 'iOS') { // 判断是ISO
  47. var isOn = undefined;
  48. var types = 0;
  49. var app = plus.ios.invoke('UIApplication', 'sharedApplication');
  50. var settings = plus.ios.invoke(app, 'currentUserNotificationSettings');
  51. if (settings) {
  52. types = settings.plusGetAttribute('types');
  53. plus.ios.deleteObject(settings);
  54. } else {
  55. types = plus.ios.invoke(app, 'enabledRemoteNotificationTypes');
  56. }
  57. plus.ios.deleteObject(app);
  58. isOn = (0 != types);
  59. if (isOn == false) {
  60. uni.showModal({
  61. title: '通知权限开启提醒',
  62. content: '您还没有开启通知权限,无法接受到消息通知,请前往设置!',
  63. showCancel: false,
  64. confirmText: '去设置',
  65. success: function(res) {
  66. if (res.confirm) {
  67. var app = plus.ios.invoke('UIApplication', 'sharedApplication');
  68. var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
  69. plus.ios.invoke(app, 'openURL:', setting);
  70. plus.ios.deleteObject(setting);
  71. plus.ios.deleteObject(app);
  72. }
  73. }
  74. });
  75. }
  76. }
  77. // #endif
  78. }


版权声明:本文为CSDN博主「D_lunar」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/D_lunar/article/details/115694296

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/274483
推荐阅读
相关标签
  

闽ICP备14008679号