-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (70 loc) · 2.67 KB
/
index.js
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
import { AppRegistry, Linking, LogBox } from 'react-native'
import messaging from '@react-native-firebase/messaging'
import AsyncStorage from '@react-native-async-storage/async-storage'
import notifee, { EventType } from '@notifee/react-native'
import { ACTION_PRESS_IDS, CLOCKIFY_URL, MESSAGES_KEY } from 'constants/global'
import { name as appName } from './app.json'
// This should be called as soon as possible
messaging().setBackgroundMessageHandler(async (message) => {
if (__DEV__) {
console.log('Background Message received:', JSON.stringify(message, null, 2))
}
// Save messages in the device to be fired once we get out of background - see App.tsx
try {
const messages = await AsyncStorage.getItem(MESSAGES_KEY)
if (messages == null) {
await AsyncStorage.setItem(MESSAGES_KEY, JSON.stringify([message]))
throw new Error(`Invalid ${MESSAGES_KEY} state after read called from setBackgroundMessageHandler`)
}
const parsed = JSON.parse(messages)
if (Array.isArray(parsed)) {
await AsyncStorage.setItem(MESSAGES_KEY, JSON.stringify([...parsed, message]))
} else {
await AsyncStorage.setItem(MESSAGES_KEY, JSON.stringify([message]))
}
} catch (error) {
if (__DEV__) {
console.log(error)
}
}
})
// handle to notification interactions
notifee.onBackgroundEvent(async ({ type, detail }) => {
const { notification, pressAction } = detail
try {
if (type === EventType.ACTION_PRESS && pressAction?.id) {
if (pressAction.id === ACTION_PRESS_IDS.openClockify) {
await Linking.openURL(CLOCKIFY_URL)
}
}
} catch (error) {
if (__DEV__) {
console.log('error trying to handle action', error)
}
} finally {
if (type === EventType.ACTION_PRESS && pressAction?.id !== ACTION_PRESS_IDS.openClockify) {
// close the notification
await notifee.cancelNotification(notification.id)
}
}
})
function HeadlessCheck ({ isHeadless }) {
if (isHeadless) {
// App has been launched in the background by iOS, ignore
return null
}
require('react-native-gesture-handler')
const React = require('react')
const { App } = require('./src/App')
if (__DEV__) {
LogBox.ignoreLogs([
// This warning occurs because we pass callbacks in some route navigations i.e. Dialog
// and because fuctions are not serializable, just remember taht state persistance and deep-linking
// features became unavailable for those screens
'Non-serializable values were found in the navigation state',
])
import('./src/util/reactotronConfig').then(() => console.log('Reactotron Configured'))
}
return <App />
}
AppRegistry.registerComponent(appName, () => HeadlessCheck)