forked from ProEpiDesenvolvimento/guardioes-app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
83 lines (70 loc) · 2.73 KB
/
App.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
import React, { useEffect, useState } from 'react'
import { Alert } from 'react-native'
import { ONESIGNAL_APP_ID } from 'react-native-dotenv'
import OneSignal from 'react-native-onesignal'
import { enableScreens } from 'react-native-screens'
import { NavigationContainer } from '@react-navigation/native'
import AppProvider from './src/hooks'
import Routes from './src/routes'
import './src/config/ReactotronConfig'
enableScreens()
const Guardioes = () => {
const [isSubscribed, setIsSubscribed] = useState(null)
useEffect(() => {
const initOneSignal = async () => {
/* ONESIGNAL SETUP */
OneSignal.setAppId(ONESIGNAL_APP_ID)
OneSignal.setLogLevel(5, 0)
OneSignal.promptForPushNotificationsWithUserResponse((response) => {
console.log('Prompt response:', response)
})
/* ONESIGNAL HANDLERS */
OneSignal.setNotificationWillShowInForegroundHandler(
(notifReceivedEvent) => {
console.log(
'OneSignal: notification will show in foreground:',
notifReceivedEvent
)
const notif = notifReceivedEvent.getNotification()
const button1 = {
text: 'Cancel',
onPress: () => {
notifReceivedEvent.complete()
},
style: 'cancel',
}
const button2 = {
text: 'Confirm',
onPress: () => {
notifReceivedEvent.complete(notif)
},
}
Alert.alert(notif.title, notif.body, [button1, button2], {
cancelable: false,
})
}
)
OneSignal.setNotificationOpenedHandler((notification) => {
console.log('OneSignal: notification opened:', notification)
})
OneSignal.addSubscriptionObserver((event) => {
console.log('OneSignal: subscription changed:', event)
setIsSubscribed(event.to.isSubscribed)
})
OneSignal.addPermissionObserver((event) => {
console.log('OneSignal: permission changed:', event)
})
const deviceState = await OneSignal.getDeviceState()
setIsSubscribed(deviceState.isSubscribed)
}
initOneSignal()
}, [])
return (
<NavigationContainer>
<AppProvider>
<Routes />
</AppProvider>
</NavigationContainer>
)
}
export default Guardioes