forked from chmod77/react-native_wallet_sdk_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (153 loc) · 4.42 KB
/
Copy pathindex.js
File metadata and controls
159 lines (153 loc) · 4.42 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Copyright (c) 2019 CYBAVO, Inc.
* https://www.cybavo.com
*
* All rights reserved.
*/
import {
PushNotification as CYBAVOPushNotification,
Wallets,
WalletSdk,
} from '@cybavo/react-native-wallet-service';
import { AppRegistry, Platform, AsyncStorage } from 'react-native';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import AppWrapper from './AppWrapper';
import { GoogleSignin } from 'react-native-google-signin';
import { name as appName } from './app.json';
import * as WeChat from 'react-native-wechat-lib';
import {
SERVICE_ENDPOINT,
SERVICE_API_CODE,
SERVICE_API_CODE_IOS,
GOOGLE_SIGN_IN_WEB_CLI_ID,
GOOGLE_SENDER_ID,
WECHAT_SIGN_IN_APP_ID,
WECHAT_UNIVERSAL_LINK,
} from './BuildConfig.json';
import RNPushNotification from 'react-native-push-notification';
// init wallet SDK
WalletSdk.init({
endpoint: SERVICE_ENDPOINT,
apiCode: Platform.OS === 'android' ? SERVICE_API_CODE : SERVICE_API_CODE_IOS,
apnsSandbox: true,
});
// init Google Sign-in
GoogleSignin.configure({
offlineAccess: false,
webClientId: GOOGLE_SIGN_IN_WEB_CLI_ID,
});
// init WeChat Sign-in
WeChat.registerApp(WECHAT_SIGN_IN_APP_ID, WECHAT_UNIVERSAL_LINK).then();
const localNotificatiopnIos = notification => {
// required on iOS only (see fetchCompletionHandler docs: https://facebook.github.io/react-native/docs/pushnotificationios.html)
notification.finish(PushNotificationIOS.FetchResult.NoData);
if (
notification.data &&
notification.data.data &&
notification.data.data.jsonBody
) {
let data = CYBAVOPushNotification.parse(
JSON.stringify(notification.data.data.jsonBody)
);
if (data.direction == Wallets.Transaction.Direction.IN) {
RNPushNotification.localNotification({
id: '120',
title: 'Transaction Received',
message: 'Amount ' + data.amount + ' from ' + data.fromAddress,
vibrate: true,
});
} else {
RNPushNotification.localNotification({
id: '121',
title: 'Transaction Sent',
message: 'Amount ' + data.amount + ' to ' + data.toAddress,
vibrate: true,
});
}
console.debug(
'onNotification' +
Wallets.Transaction.Direction.IN +
',' +
data.walletID +
',' +
data.type +
',' +
data.fee +
',' +
data.currency +
',' +
data.timestamp +
',' +
data.tokenAddress
);
}
};
const localNotificatiopnAndroid = notification => {
let jsonbody =
notification['pinpoint.jsonBody'] ||
(notification.data && notification.data['pinpoint.jsonBody']);
if (jsonbody) {
let data = CYBAVOPushNotification.parse(jsonbody);
if (data.direction == Wallets.Transaction.Direction.IN) {
RNPushNotification.localNotification({
id: '120',
title: 'Transaction Received',
message: 'Amount ' + data.amount + ' from ' + data.fromAddress,
vibrate: true,
});
} else {
RNPushNotification.localNotification({
id: '121',
title: 'Transaction Sent',
message: 'Amount ' + data.amount + ' to ' + data.toAddress,
vibrate: true,
});
}
console.debug(
'onNotification' +
Wallets.Transaction.Direction.IN +
',' +
data.walletID +
',' +
data.type +
',' +
data.fee +
',' +
data.currency +
',' +
data.timestamp +
',' +
data.tokenAddress
);
}
};
//init push
RNPushNotification.configure({
popInitialNotification: true,
requestPermissions: true,
onRegister: async token => {
console.log('onRegister:', token.token);
AsyncStorage.setItem('pushDeviceToken', token.token).then(() =>
console.debug('save pushDeviceToken done')
);
},
senderID: GOOGLE_SENDER_ID,
onNotification: notification => {
console.log('onNotification', notification);
if (Platform.OS === 'ios') {
localNotificatiopnIos(notification);
} else {
localNotificatiopnAndroid(notification);
}
},
});
export function getPushToken() {
if (Platform.OS === 'ios') {
return AsyncStorage.getItem('pushDeviceToken');
} else {
// use firebsae instance id since for some reason react-native-push-notification onRegister not invoked
// TODO: may need to return APNS token for iOS
return AsyncStorage.getItem('pushDeviceToken');
}
}
AppRegistry.registerComponent(appName, () => AppWrapper);