index.js 5.8 KB

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