123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- /**
- * Created by zhangzy on 16/7/27.
- */
- 'use strict';
- import {
- NativeModules,
- Platform,
- NativeEventEmitter,
- PermissionsAndroid,
- } from 'react-native';
- const MIPushModule = NativeModules.MIPushModule;
- import PushNotificationIOS from '@react-native-community/push-notification-ios'
- /**
- * 获取app的版本名称\版本号和渠道名
- */
- class MIPush extends NativeEventEmitter {
- // 构造
- constructor(props) {
- super(MIPushModule);
- // 初始状态
- this.state = {};
- }
- init(appid, appkey) {
- if (Platform.OS == 'android') {
- PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE).then((state) => {
- if (state) {
- PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((state) => {
- if (state) {
- MIPushModule.init(appid, appkey);
- } else {
- PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE).then((granted) => {
- if (granted === PermissionsAndroid.RESULTS.GRANTED) {
- MIPushModule.init(appid, appkey);
- }
- });
- }
- });
- } else {
- PermissionsAndroid.requestMultiple([
- PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
- PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
- ]).then((granted) => {
- // if (granted === PermissionsAndroid.RESULTS.GRANTED) {
- MIPushModule.init(appid, appkey);
- // }
- });
- }
- });
- }
- }
- /**
- * 设置别名
- * @param text
- */
- setAlias(text) {
- MIPushModule.setAlias(text);
- }
- /**
- * 注销别名
- * @param text
- */
- unsetAlias(text) {
- MIPushModule.unsetAlias(text);
- }
- /**
- * 设置主题,类似tag
- * @param text
- */
- subscribe(text) {
- MIPushModule.subscribe(text);
- }
- /**
- * 注销主题
- * @param text
- */
- unsubscribe(text) {
- MIPushModule.unsubscribe(text);
- }
- /**
- * 设置账号,一个账号需要多台设备接收通知
- * @param text
- */
- setAccount(text) {
- MIPushModule.setAccount(text);
- }
- /**
- * 注销账号
- * @param text
- */
- unsetAccount(text) {
- MIPushModule.unsetAccount(text);
- }
- /**
- *
- * @param type
- * ios :
- * notification => 监听收到apns通知
- * localNotification => 监听收到本地通知
- * register => 注册deviceToken 通知
- *
- * android :
- * xmpush_notify => 监听收到推送
- * xmpush_click => 监听推送被点击
- * xmpush_message => 监听收到透传消息
- * @param handler
- */
- addEventListener(type, handler) {
- if (Platform.OS == 'ios') {
- switch (type) {
- case 'notification':
- case 'localNotification':
- case 'register':
- PushNotificationIOS.addEventListener(type, handler);
- break;
- default:
- this.addListener(type, handler);
- break;
- }
- } else {
- this.addListener(type, handler);
- }
- }
- removeEventListener(type) {
- if (Platform.OS == 'ios') {
- switch (type) {
- case 'notification':
- case 'localNotification':
- case 'register':
- PushNotificationIOS.removeEventListener(type);
- break;
- default:
- this.removeListener(type);
- break;
- }
- } else {
- this.removeListener(type);
- }
- }
- /**
- * 发送一个本地通知
- * @param notification
- */
- presentLocalNotification(notification) {
- if (Platform.OS == 'ios') {
- PushNotificationIOS.presentLocalNotification({
- alertBody: notification.alertBody,
- alertAction: '查看',
- category: 'push',
- userInfo: notification.userInfo,
- });
- } else {
- MIPushModule.presentLocalNotification({});
- }
- }
- /**
- * 清除指定通知
- * @param notifyId
- * ios : userInfo
- * android : id
- */
- clearNotification(notifyId) {
- if (Platform.OS == 'ios') {
- PushNotificationIOS.cancelLocalNotifications(notifyId);
- } else {
- MIPushModule.clearNotification(notifyId);
- }
- }
- /**
- * 清除所有通知
- */
- clearNotifications() {
- if (Platform.OS == 'ios') {
- PushNotificationIOS.cancelAllLocalNotifications();
- } else {
- MIPushModule.clearAllNotification();
- }
- }
- /**
- * 设置角标,仅支持ios
- * @param num
- */
- setBadgeNumber(num) {
- if (Platform.OS == 'ios') {
- PushNotificationIOS.setApplicationIconBadgeNumber(num);
- }
- }
- /**
- * 通过点击通知启动app
- * @param handler
- */
- getInitialNotification(handler) {
- if (Platform.OS == 'ios') {
- PushNotificationIOS.getInitialNotification()
- .then(handler);
- } else {
- MIPushModule.getInitialNotification()
- .then(handler);
- }
- }
- }
- MIPush = new MIPush();
- module.exports = MIPush;
|