-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
98 lines (75 loc) · 3.21 KB
/
Copy pathApp.tsx
File metadata and controls
98 lines (75 loc) · 3.21 KB
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
import React, { useEffect, useState, useContext, createContext } from 'react';
import { StyleSheet, Text, View, Button, NativeModules } from 'react-native';
import FoundationService from "./foundationService"
import { Notifications } from 'react-native-notifications';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './HomeScreen';
import ProfileScreen from './ProfileScreen';
const Stack = createNativeStackNavigator();
export const TokenContext = createContext<any>({})
export default function App() {
const [token, setToken] = useState({})
const value = {
token,
setToken,
}
function initialiseComapi(deviceToken: any) {
setToken(deviceToken);
console.log("Inintialising COMAPI ...")
// initialise sdk
FoundationService.initialise()
.then(() => {
// Start a session
return FoundationService.startSession();
})
.then(() => {
// register the device token
return FoundationService.setPushDetails(deviceToken);
});
}
function initialisePush() {
// I'm using this to aquire a device token - there are several alternative packages available.
// We request a device token on startup and pass it to DotDigital. Both these taskas are asynchronous (including initialising the sdk)
// I have chained these together so as to avoid any race conditions.
Notifications.registerRemoteNotifications();
Notifications.events().registerRemoteNotificationsRegistered((event) => {
initialiseComapi(event.deviceToken, setToken);
});
Notifications.events().registerRemoteNotificationsRegistrationFailed((event) => {
console.error(event);
});
Notifications.events().registerNotificationReceivedForeground((notification, completion) => {
console.log("Notification Received - Foreground", notification.payload);
// Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
completion({ alert: true, sound: true, badge: false });
});
Notifications.events().registerNotificationOpened((notification, completion, action) => {
console.log("Notification opened by device user", notification.payload);
console.log(`Notification opened with an action identifier: ${action.identifier} and response text: ${action.text}`);
completion();
});
Notifications.events().registerNotificationReceivedBackground((notification, completion) => {
console.log("Notification Received - Background", notification.payload);
// Calling completion on iOS with `alert: true` will present the native iOS inApp notification.
completion({ alert: true, sound: true, badge: false });
});
}
useEffect(() => {
initialisePush();
}, []);
return (
<TokenContext.Provider value={value}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{ title: 'Push Test' }}
/>
<Stack.Screen name="Profile" component={ProfileScreen} />
</Stack.Navigator>
</NavigationContainer>
</TokenContext.Provider>
);
}