index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Created by zhangzy on 16/7/27.
  3. */
  4. 'use strict';
  5. import {
  6. NativeModules,
  7. Platform,
  8. PushNotificationIOS
  9. } from 'react-native';
  10. const NativeEventEmitter = require('NativeEventEmitter');
  11. var 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. PushNotificationIOS.addEventListener(type, handler);
  81. } else {
  82. this.addListener(type, handler);
  83. }
  84. }
  85. removeEventListener(type) {
  86. if(Platform.OS == 'ios') {
  87. PushNotificationIOS.removeEventListener(type);
  88. } else {
  89. this.removeEventListener(type);
  90. }
  91. }
  92. /**
  93. * 发送一个本地通知
  94. * @param notification
  95. */
  96. presentLocalNotification(notification) {
  97. if(Platform.OS == 'ios') {
  98. PushNotificationIOS.presentLocalNotification({
  99. alertBody: notification.alertBody,
  100. alertAction: "查看",
  101. category: "push",
  102. userInfo: notification.userInfo
  103. });
  104. } else {
  105. MIPushModule.presentLocalNotification({
  106. });
  107. }
  108. }
  109. /**
  110. * 清除指定通知
  111. * @param notifyId
  112. * ios : userInfo
  113. * android : id
  114. */
  115. clearNotification(notifyId) {
  116. if(Platform.OS == 'ios') {
  117. PushNotificationIOS.cancelLocalNotifications(notifyId);
  118. } else {
  119. MIPushModule.clearNotification(notifyId);
  120. }
  121. }
  122. /**
  123. * 清除所有通知
  124. */
  125. clearNotifications() {
  126. if(Platform.OS == 'ios') {
  127. PushNotificationIOS.cancelAllLocalNotifications();
  128. } else {
  129. MIPushModule.clearAllNotification();
  130. }
  131. }
  132. /**
  133. * 设置角标,仅支持ios
  134. * @param num
  135. */
  136. setBadgeNumber(num) {
  137. if(Platform.OS == 'ios') {
  138. PushNotificationIOS.setApplicationIconBadgeNumber(num);
  139. }
  140. }
  141. /**
  142. * 通过点击通知启动app
  143. * @param handler
  144. */
  145. getInitialNotification(handler) {
  146. if(Platform.OS == 'ios') {
  147. PushNotificationIOS.getInitialNotification()
  148. .then(handler);
  149. } else {
  150. MIPushModule.getInitialNotification()
  151. .then(handler);
  152. }
  153. }
  154. }
  155. MIPush = new MIPush();
  156. module.exports = MIPush;