native_permission_check.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { getLocalPush } from '@/features/trackTimeDuration/actions/TrackTimeActions';
  2. import { clientInfo } from '@/services/common';
  3. import Taro from '@tarojs/taro';
  4. import JPush from 'jpush-react-native';
  5. import { Alert, Linking } from 'react-native';
  6. import { check, PERMISSIONS, RESULTS, checkMultiple } from 'react-native-permissions';
  7. // import {OneSignal} from 'react-native-onesignal';
  8. export const checkLocation = () => {
  9. // JPush.isNotificationEnabled((result) => {
  10. // Taro.showModal({
  11. // title: '提示',
  12. // content: JSON.stringify(result)
  13. // })
  14. // });
  15. check(PERMISSIONS.IOS.LOCATION_ALWAYS)
  16. .then((result) => {
  17. switch (result) {
  18. case RESULTS.UNAVAILABLE:
  19. // Alert.alert('This feature is not available (on this device / in this context)');
  20. break;
  21. case RESULTS.DENIED:
  22. console.log('The permission has not been requested / is denied but requestable');
  23. break;
  24. case RESULTS.LIMITED:
  25. console.log('The permission is limited: some actions are possible');
  26. break;
  27. case RESULTS.GRANTED:
  28. console.log('The permission is granted');
  29. break;
  30. case RESULTS.BLOCKED:
  31. // console.log('The permission is denied and not requestable anymore');
  32. break;
  33. }
  34. })
  35. .catch((error) => {
  36. // …
  37. });
  38. }
  39. export const uploadPermissions = () => {
  40. if (Taro.getSystemInfoSync().platform == 'android') {
  41. checkMultiple([PERMISSIONS.ANDROID.CAMERA,
  42. PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
  43. PERMISSIONS.ANDROID.POST_NOTIFICATIONS]).then(statuses => {
  44. const OneSignal = require('react-native-onesignal').OneSignal
  45. OneSignal.initialize("2bbf5339-7ab8-4c5c-9a66-c3a8eaea188e");
  46. OneSignal.Notifications.getPermissionAsync().then((res) => {
  47. if (res) {
  48. global.notification = 'authorized'
  49. }
  50. else {
  51. global.notification = 'denied'
  52. }
  53. clientInfo({
  54. perm: {
  55. camera: statuses[PERMISSIONS.ANDROID.CAMERA],
  56. location: statuses[PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION],
  57. notification: global.notification
  58. }
  59. })
  60. })
  61. });
  62. return
  63. }
  64. checkMultiple([PERMISSIONS.IOS.CAMERA,
  65. PERMISSIONS.IOS.PHOTO_LIBRARY,
  66. PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
  67. PERMISSIONS.IOS.LOCATION_ALWAYS]).then(statuses => {
  68. if (global.notification == 'not_determined') {
  69. clientInfo({
  70. perm: {
  71. album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
  72. camera: statuses[PERMISSIONS.IOS.CAMERA],
  73. location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
  74. location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
  75. notification: 'not_determined'
  76. }
  77. })
  78. }
  79. else {
  80. JPush.isNotificationEnabled((res) => {
  81. console.log(res)
  82. if (res) {
  83. getLocalPush()
  84. clientInfo({
  85. perm: {
  86. album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
  87. camera: statuses[PERMISSIONS.IOS.CAMERA],
  88. location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
  89. location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
  90. notification: 'authorized'
  91. }
  92. })
  93. }
  94. else {
  95. clientInfo({
  96. perm: {
  97. album: statuses[PERMISSIONS.IOS.PHOTO_LIBRARY],
  98. camera: statuses[PERMISSIONS.IOS.CAMERA],
  99. location_always: statuses[PERMISSIONS.IOS.LOCATION_ALWAYS],
  100. location_when_in_use: statuses[PERMISSIONS.IOS.LOCATION_WHEN_IN_USE],
  101. notification: 'denied'
  102. }
  103. })
  104. }
  105. })
  106. }
  107. })
  108. }
  109. export const checkNotification = () => {
  110. if (Taro.getSystemInfoSync().platform == 'android') {
  111. const OneSignal = require('react-native-onesignal').OneSignal
  112. OneSignal.Notifications.canRequestPermission().then((res) => {
  113. if (res) {
  114. OneSignal.Notifications.requestPermission(true);
  115. }
  116. else {
  117. Linking.openSettings()
  118. }
  119. })
  120. return;
  121. }
  122. if (global.notification == 'not_determined') {
  123. if (Taro.getSystemInfoSync().platform == 'ios') {
  124. var Jto = require('react-native').NativeModules.NativeBridge;
  125. Jto.authNotification()
  126. }
  127. else {
  128. // Linking.openSettings()
  129. const OneSignal = require('react-native-onesignal').OneSignal
  130. OneSignal.Notifications.requestPermission(true).then((response) => {
  131. if (response) {
  132. global.notification = 'authorized'
  133. uploadPermissions()
  134. } else {
  135. global.notification = 'denied'
  136. uploadPermissions()
  137. }
  138. });
  139. }
  140. // const test = require('@/utils/push').default
  141. // test()
  142. }
  143. else {
  144. if (Taro.getSystemInfoSync().platform == 'ios') {
  145. Linking.openURL('app-settings:notifications')
  146. }
  147. else {
  148. Linking.openSettings()
  149. }
  150. }
  151. }