-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
96 lines (76 loc) · 2.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Created by qiepeipei on 17/6/22.
*/
'use strict';
import React, { PropTypes, Component } from 'react';
import {
DeviceEventEmitter,
NativeModules,
Platform,
NativeAppEventEmitter,
AppState
} from 'react-native'
var PushObj = NativeModules.BaiDuPushManager;
class BaiDuPush{
constructor () {
this.ReceiveMessageObj = null;
this.BackstageMessageObj = null;
}
//监听前台消息
monitorReceiveMessage(callBack){
//处于后台时,拦截收到的消息
// if(AppState.currentState == 'background') {
// return;
// }
if(Platform.OS == 'ios'){
this.ReceiveMessageObj = NativeAppEventEmitter.addListener(
PushObj.DidReceiveMessage,callBack);
}else{
this.ReceiveMessageObj = DeviceEventEmitter.addListener(
PushObj.DidReceiveMessage,(data)=>{
let obj = {};
obj.title = data.title;
obj.description = data.description;
obj.customContentString = JSON.parse(data.customContentString);
callBack(obj);
});
}
}
//监听后台打开消息
monitorBackstageOpenMessage(callBack){
if(Platform.OS == 'ios'){
this.BackstageMessageObj = NativeAppEventEmitter.addListener(
PushObj.DidOpenMessage,callBack);
}else{
this.BackstageMessageObj = DeviceEventEmitter.addListener(
PushObj.DidOpenMessage,(data)=>{
let obj = {};
obj.title = data.title;
obj.description = data.description;
obj.customContentString = JSON.parse(data.customContentString);
callBack(obj);
});
}
}
//取消消息监听
monitorMessageCancel(){
if(this.ReceiveMessageObj){
this.ReceiveMessageObj.remove();
}
if(this.BackstageMessageObj){
this.BackstageMessageObj.remove();
}
}
//获取ChannelId
async getChannelId(){
try {
return await PushObj.getChannelId();
} catch (e) {
return null;
}
}
testSend(){
PushObj.testPrint("hello world");
}
}
export default new BaiDuPush();