-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
132 lines (119 loc) · 4.66 KB
/
App.tsx
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import * as React from 'react';
import {PermissionsAndroid, Platform, SafeAreaView, StyleSheet} from 'react-native';
import PushNotification, {Importance} from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import store from './store';
import {Provider} from 'react-redux';
import AppStrings from './src/constants/AppStrings';
import Navigation from './src/navigation/Navigation';
import {RouteNames} from './src/navigation/Routes';
import { showToast } from './src/constants/commonUtils';
export default class App extends React.Component {
componentDidMount(): void {
this.checkApplicationPermission();
PushNotification.createChannel(
{
channelId: 'nxtlevel_channel_id', // Replace with your chosen channel ID
channelName: 'nxtlevel Notifications',
importance: Importance.HIGH, // Adjust importance level as needed
vibrate: true, // Enable vibration for notifications
},
(created: any) => console.log(`Channel created: ${created}`),
);
PushNotification.configure({
invokeApp: true,
// (optional) Called when Token is generated (iOS and Android)
onRegister: async function (token: any) {
console.log(token)
await AsyncStorage.setItem(AppStrings.FCM_TOKEN, token.token);
},
onNotification: function (notification) {
console.log('NOTIFICATION:', notification);
// Check if the app is in the foreground (open)
if (notification.foreground) {
console.log('foreground');
// Display a local notification when the app is open
PushNotification.localNotification({
title: notification.title,
message: notification.message,
channelId: 'nxtlevel_channel_id',
});
} else {
if (notification.userInteraction) {
// Notification was clicked, navigate to the desired screen
this.props.navigation.navigate(RouteNames.SplashScreen); // Replace with the desired screen
}
}
// Finish processing the notification
notification.finish(PushNotificationIOS.FetchResult.NoData);
},
// (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)
onAction: function (notification: {action: any}) {
console.log('ACTION:', notification.action);
console.log('NOTIFICATION:', notification);
// process the action
},
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: function (err: {message: any}) {
console.error(err.message, err);
},
// ANDROID ONLY: GCM or FCM Sender ID (product_number) (optional - not required for local notifications, but is need to receive remote push notifications)
senderID: '1096238675733',
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
requestPermissions: true,
});
// Simulate a dummy notification for testing
// setTimeout(() => {
// PushNotification.localNotification({
// title: 'Test Notification',
// message: 'This is a test notification!',
// channelId: 'nxtlevel_channel_id',
// });
// }, 5000); // Send the notification after 5 seconds
}
checkApplicationPermission = async () => {
const version = Platform.Version;
if (Platform.OS === 'android' && version > 31) {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
showToast('Notification permission allowed');
} else {
showToast('Notification permission denied');
}
} catch (error) {
}
}
}
render() {
return (
<SafeAreaView style={styles.container}>
<Provider store={store}>
<Navigation />
</Provider>
</SafeAreaView>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});