index.js 4.3 KB

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