index.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Created by zhangzy on 16/7/27.
  3. */
  4. 'use strict';
  5. import {
  6. NativeModules,
  7. Platform,
  8. NativeEventEmitter,
  9. PermissionsAndroid,
  10. } from 'react-native';
  11. const MIPushModule = NativeModules.MIPushModule;
  12. import PushNotificationIOS from '@react-native-community/push-notification-ios'
  13. /**
  14. * 获取app的版本名称\版本号和渠道名
  15. */
  16. class MIPush extends NativeEventEmitter {
  17. // 构造
  18. constructor(props) {
  19. super(MIPushModule);
  20. // 初始状态
  21. this.state = {};
  22. }
  23. init(appid, appkey) {
  24. if (Platform.OS == 'android') {
  25. PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((state) => {
  26. if (state) {
  27. PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((state) => {
  28. if (state) {
  29. MIPushModule.init(appid, appkey);
  30. } else {
  31. PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((granted) => {
  32. if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  33. MIPushModule.init(appid, appkey);
  34. }
  35. });
  36. }
  37. });
  38. } else {
  39. PermissionsAndroid.requestMultiple([
  40. PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
  41. PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
  42. ]).then((granted) => {
  43. // if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  44. MIPushModule.init(appid, appkey);
  45. // }
  46. });
  47. }
  48. });
  49. }
  50. }
  51. /**
  52. * 设置别名
  53. * @param text
  54. */
  55. setAlias(text) {
  56. MIPushModule.setAlias(text);
  57. }
  58. /**
  59. * 注销别名
  60. * @param text
  61. */
  62. unsetAlias(text) {
  63. MIPushModule.unsetAlias(text);
  64. }
  65. /**
  66. * 设置主题,类似tag
  67. * @param text
  68. */
  69. subscribe(text) {
  70. MIPushModule.subscribe(text);
  71. }
  72. /**
  73. * 注销主题
  74. * @param text
  75. */
  76. unsubscribe(text) {
  77. MIPushModule.unsubscribe(text);
  78. }
  79. /**
  80. * 设置账号,一个账号需要多台设备接收通知
  81. * @param text
  82. */
  83. setAccount(text) {
  84. MIPushModule.setAccount(text);
  85. }
  86. /**
  87. * 注销账号
  88. * @param text
  89. */
  90. unsetAccount(text) {
  91. MIPushModule.unsetAccount(text);
  92. }
  93. /**
  94. *
  95. * @param type
  96. * ios :
  97. * notification => 监听收到apns通知
  98. * localNotification => 监听收到本地通知
  99. * register => 注册deviceToken 通知
  100. *
  101. * android :
  102. * xmpush_notify => 监听收到推送
  103. * xmpush_click => 监听推送被点击
  104. * xmpush_message => 监听收到透传消息
  105. * @param handler
  106. */
  107. addEventListener(type, handler) {
  108. if (Platform.OS == 'ios') {
  109. switch (type) {
  110. case 'notification':
  111. case 'localNotification':
  112. case 'register':
  113. PushNotificationIOS.addEventListener(type, handler);
  114. break;
  115. default:
  116. this.addListener(type, handler);
  117. break;
  118. }
  119. } else {
  120. this.addListener(type, handler);
  121. }
  122. }
  123. removeEventListener(type) {
  124. if (Platform.OS == 'ios') {
  125. switch (type) {
  126. case 'notification':
  127. case 'localNotification':
  128. case 'register':
  129. PushNotificationIOS.removeEventListener(type);
  130. break;
  131. default:
  132. this.removeListener(type);
  133. break;
  134. }
  135. } else {
  136. this.removeListener(type);
  137. }
  138. }
  139. /**
  140. * 发送一个本地通知
  141. * @param notification
  142. */
  143. presentLocalNotification(notification) {
  144. if (Platform.OS == 'ios') {
  145. PushNotificationIOS.presentLocalNotification({
  146. alertBody: notification.alertBody,
  147. alertAction: '查看',
  148. category: 'push',
  149. userInfo: notification.userInfo,
  150. });
  151. } else {
  152. MIPushModule.presentLocalNotification({});
  153. }
  154. }
  155. /**
  156. * 清除指定通知
  157. * @param notifyId
  158. * ios : userInfo
  159. * android : id
  160. */
  161. clearNotification(notifyId) {
  162. if (Platform.OS == 'ios') {
  163. PushNotificationIOS.cancelLocalNotifications(notifyId);
  164. } else {
  165. MIPushModule.clearNotification(notifyId);
  166. }
  167. }
  168. /**
  169. * 清除所有通知
  170. */
  171. clearNotifications() {
  172. if (Platform.OS == 'ios') {
  173. PushNotificationIOS.cancelAllLocalNotifications();
  174. } else {
  175. MIPushModule.clearAllNotification();
  176. }
  177. }
  178. /**
  179. * 设置角标,仅支持ios
  180. * @param num
  181. */
  182. setBadgeNumber(num) {
  183. if (Platform.OS == 'ios') {
  184. PushNotificationIOS.setApplicationIconBadgeNumber(num);
  185. }
  186. }
  187. /**
  188. * 通过点击通知启动app
  189. * @param handler
  190. */
  191. getInitialNotification(handler) {
  192. if (Platform.OS == 'ios') {
  193. PushNotificationIOS.getInitialNotification()
  194. .then(handler);
  195. } else {
  196. MIPushModule.getInitialNotification()
  197. .then(handler);
  198. }
  199. }
  200. }
  201. MIPush = new MIPush();
  202. module.exports = MIPush;