This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
notifications.ts
159 lines (135 loc) · 4.08 KB
/
notifications.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
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
import type { RefObject } from 'react'
import AsyncStorage from '@react-native-async-storage/async-storage'
import type { PushNotificationPermissions } from 'react-native'
import { Platform } from 'react-native'
import Config from 'react-native-config'
// https://dev.to/edmondso006/react-native-local-ios-and-android-notifications-2c58
import PushNotification from 'react-native-push-notification'
import { dispatch } from 'app/store'
import { open } from 'app/store/notifications/actions'
import type { MessagePostingWebView } from 'app/types/MessagePostingWebView'
import { EventNames } from 'app/types/analytics'
import { track, make } from 'app/utils/analytics'
type Token = {
token: string
os: string
}
// Set to true while the push notification service is registering with the os
let isRegistering = false
// Reference to hold the web ref to push routes to
let webRef: RefObject<MessagePostingWebView>
const getPlatformConfiguration = () => {
if (Platform.OS === 'android') {
console.info('Fcm Sender ID:', Config.FCM_SENDER_ID)
return {
senderID: Config.FCM_SENDER_ID,
requestPermissions: true,
largeIcon: 'ic_launcher',
smallIcon: 'ic_notification'
}
} else {
return {
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true
},
// Turn the initial permissions request off
requestPermissions: false
}
}
}
// Singleton class
class PushNotifications {
lastId: number
token: Token | null
// onNotification is a function passed in that is to be called when a
// notification is to be emitted.
constructor() {
this.configure()
this.lastId = 0
this.token = null
}
setWebRef(w: RefObject<MessagePostingWebView>) {
webRef = w
}
onNotification(notification: any) {
console.info(`Received notification ${JSON.stringify(notification)}`)
if (notification.userInteraction || Platform.OS === 'android') {
track(
make({
eventName: EventNames.NOTIFICATIONS_OPEN_PUSH_NOTIFICATION,
...(notification.message
? {
title: notification.message.title,
body: notification.message.body
}
: {})
})
)
if (!webRef || !webRef.current) return
dispatch(open())
}
}
async onRegister(token: Token) {
console.log('REGISTER DEVICE TOKEN', token)
this.token = token
await AsyncStorage.setItem('@device_token', JSON.stringify(token))
isRegistering = false
}
deregister() {
AsyncStorage.removeItem('@device_token')
}
async configure() {
PushNotification.configure({
onNotification: this.onNotification,
onRegister: this.onRegister,
popInitialNotification: false,
...getPlatformConfiguration()
})
try {
const token = await AsyncStorage.getItem('@device_token')
if (token) {
this.token = JSON.parse(token)
} else {
console.info(`Device token not found`)
}
} catch (e) {
console.error(`Device token read error`)
}
}
requestPermission() {
isRegistering = true
PushNotification.requestPermissions()
}
checkPermission(
callback: (permissions: PushNotificationPermissions) => void
) {
return PushNotification.checkPermissions(callback)
}
cancelNotif() {
PushNotification.cancelLocalNotifications({ id: '' + this.lastId })
}
cancelAll() {
PushNotification.cancelAllLocalNotifications()
}
setBadgeCount(count: number) {
PushNotification.setApplicationIconBadgeNumber(count)
}
async getToken() {
// Wait until the device token and OS are persisted to async storage
// isRegistering modified as global
// eslint-disable-next-line no-unmodified-loop-condition
while (isRegistering) {
await new Promise((resolve) => setTimeout(resolve, 100))
}
const token = await AsyncStorage.getItem('@device_token')
if (token) {
return JSON.parse(token)
}
return {}
}
}
const notifications = new PushNotifications()
export default notifications