-
Notifications
You must be signed in to change notification settings - Fork 1
/
PushNotificationManager.ts
94 lines (78 loc) · 2.45 KB
/
PushNotificationManager.ts
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
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification, {
ReceivedNotification,
} from 'react-native-push-notification';
export type AnyNotification = Pick<
ReceivedNotification,
| 'foreground'
| 'userInteraction'
| 'message'
| 'data'
| 'subText'
| 'badge'
| 'alert'
| 'sound'
| 'id'
| 'action'
| 'finish'
>;
type NotificationForwardingFunction = (arg0: AnyNotification) => void;
class PushNotificationManager {
forwardNotificationFn: NotificationForwardingFunction | null;
constructor() {
this.forwardNotificationFn = null;
}
setNotificationForwardingFunction = (
forwardNotification: NotificationForwardingFunction,
) => {
this.forwardNotificationFn = forwardNotification;
};
onNotificationRecieved = (notification: AnyNotification) => {
if (this.forwardNotificationFn) {
this.forwardNotificationFn(notification);
}
// (required) Called when a remote is received or opened, or local notification is opened
notification.finish(PushNotificationIOS.FetchResult.NoData);
};
// Must be outside of any component LifeCycle (such as `componentDidMount`).
initialize(): void {
PushNotification.configure({
onNotification: this.onNotificationRecieved,
onRegistrationError: function (err) {
console.error(err.message, err);
},
permissions: {
alert: true,
badge: true,
sound: true,
},
// Whether the library should pop the first notification when the app starts
popInitialNotification: false,
// Requestion permissions on startup
requestPermissions: true,
});
PushNotification.createChannel(
{
channelId: 'REMOTE_NOTIFICATIONS',
channelName: 'Remote Notifications',
channelDescription: 'Remote notifications for this app',
soundName: 'default',
importance: 4,
vibrate: true,
},
created => console.log(created),
);
}
async getInitialPushNotification(): Promise<ReceivedNotification | null> {
return new Promise<ReceivedNotification | null>((resolve, _) => {
PushNotification.popInitialNotification(
(notification: ReceivedNotification | null) => {
resolve(notification);
},
);
});
}
}
const NotificationManager = new PushNotificationManager();
// Singelton that everything must reference to communicate with notifications
export default NotificationManager;