forked from ZeusLN/zeus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushNotificationManager.tsx
93 lines (85 loc) · 3.58 KB
/
PushNotificationManager.tsx
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
import React from 'react';
import { Alert, Platform, View } from 'react-native';
import { Notifications } from 'react-native-notifications';
import stores from './stores/Stores';
export default class PushNotificationManager extends React.Component<any, any> {
componentDidMount() {
if (Platform.OS === 'ios') Notifications.ios.setBadgeCount(0);
this.registerDevice();
this.registerNotificationEvents();
}
registerDevice = () => {
Notifications.events().registerRemoteNotificationsRegistered(
(event) => {
const deviceToken = event.deviceToken;
console.log('Device Token Received', deviceToken);
stores.lightningAddressStore.setDeviceToken(deviceToken);
}
);
Notifications.events().registerRemoteNotificationsRegistrationFailed(
(event) => {
console.error('event-err', event);
}
);
Notifications.registerRemoteNotifications();
};
registerNotificationEvents = () => {
Notifications.events().registerNotificationReceivedForeground(
(notification, completion) => {
console.log('Notification Received - Foreground', notification);
// Don't display redeem notification if auto-redeem is on
if (
stores.settingsStore.settings?.lightningAddress
?.automaticallyAccept &&
JSON.stringify(notification.payload).includes(
'Redeem within the next 24 hours'
)
)
return;
if (Platform.OS === 'android') {
Alert.alert(
notification.payload['gcm.notification.title'],
notification.payload['gcm.notification.body'],
[
{
text: 'OK',
onPress: () => console.log('OK Pressed')
}
]
);
}
if (Platform.OS === 'ios') {
// Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
completion({ alert: true, sound: false, badge: false });
}
}
);
Notifications.events().registerNotificationOpened(
(notification, completion) => {
console.log('Notification opened by device user', notification);
console.log(
`Notification opened with an action identifier: ${notification.identifier}`
);
completion();
}
);
Notifications.events().registerNotificationReceivedBackground(
(notification, completion: any) => {
console.log('Notification Received - Background', notification);
// Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
completion({ alert: true, sound: true, badge: false });
}
);
Notifications.getInitialNotification()
.then((notification) => {
console.log('Initial notification was:', notification || 'N/A');
})
.catch((err) =>
console.error('getInitialNotifiation() failed', err)
);
};
render() {
const { children } = this.props;
return <View style={{ flex: 1 }}>{children}</View>;
}
}