-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPushNotification.js
More file actions
99 lines (93 loc) · 3.12 KB
/
PushNotification.js
File metadata and controls
99 lines (93 loc) · 3.12 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
import { PushNotification } from '@cybavo/react-native-auth-service';
import RNPushNotification from 'react-native-push-notification';
import iid from '@react-native-firebase/iid';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import { Platform } from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
import store from './store';
import { Service } from './Service';
import { newNotifications } from './store/actions/notifications';
const onRemoteNotificationIos = notification => {
console.log('onNotification', notification);
if (!notification.data || !notification.data.data) {
return;
}
const payload = notification.data.data.jsonBody;
if (!payload) {
return;
}
console.log('localNotification');
store.dispatch(newNotifications(payload));
RNPushNotification.localNotification({
id: '120',
title: 'New Action',
message: '',
vibrate: true,
});
};
/** iOS*/
PushNotificationIOS.addEventListener('register', token => {
console.log('AWSPushNotification.onRegister', token);
AsyncStorage.setItem('pushDeviceToken', token).then(() =>
console.debug('save pushDeviceToken done')
);
});
/** iOS*/
// PushNotificationIOS.addEventListener('notification', onRemoteNotificationIos);
/** iOS*/
PushNotificationIOS.addEventListener('registrationError', registrationError => {
console.log(registrationError, '--');
});
const NOTIFICATION_ID = 99;
RNPushNotification.configure({
popInitialNotification: true,
requestPermissions: true,
onRegister: async token => {
console.log('onRegister:', token);
const authenticator = await Service.get();
try {
const pairings = await authenticator.getPairings();
if (pairings.length > 0) {
await authenticator.setPushToken(token.token);
}
} catch (error) {
console.warn('authenticator.setPushToken failed', error);
}
},
onNotification: notification => {
console.log('onNotification', notification);
if (Platform.OS === 'ios') {
// required on iOS only (see fetchCompletionHandler docs:
// https://github.com/react-native-community/react-native-push-notification-ios)
notification.finish(PushNotificationIOS.FetchResult.NoData);
onRemoteNotificationIos(notification);
return;
}
const payload = notification['pinpoint.jsonBody'];
if (!payload) {
return;
}
const { devices, title, body } = PushNotification.parse(payload);
store.dispatch(newNotifications(devices));
// show local notification
RNPushNotification.localNotification({
id: NOTIFICATION_ID,
title: title,
message: body,
});
},
});
/** iOS*/
// PushNotificationIOS.requestPermissions();
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 iid().getToken();
}
}
export function clearNotifications() {
RNPushNotification.cancelLocalNotifications({ id: NOTIFICATION_ID });
}