-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
76 lines (65 loc) · 2.43 KB
/
App.tsx
File metadata and controls
76 lines (65 loc) · 2.43 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
import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View } from 'react-native';
import * as Notifications from 'expo-notifications';
import { useEffect } from 'react';
import messaging, { FirebaseMessagingTypes } from '@react-native-firebase/messaging';
export default function App() {
useEffect(() => {
async function loadNotification() {
Notifications.requestPermissionsAsync();
const token= (await Notifications.getDevicePushTokenAsync()).data;
console.log("Push token:",token);
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log("Message handled in the background!", remoteMessage);
const notification = {
title: remoteMessage.notification?.title,
body: remoteMessage.notification?.body,
data: remoteMessage.data, // optional data payload
};
// Schedule the notification with a null trigger to show immediately
await Notifications.scheduleNotificationAsync({
content: notification,
trigger: null,
});
});
// Handle push notifications when the app is in the foreground
async function handlePushNotification(remoteMessage: FirebaseMessagingTypes.RemoteMessage) {
console.log("Message handled in the foreground!", remoteMessage);
console.log(remoteMessage);
const notification = {
title: remoteMessage.notification?.title,
body: remoteMessage.notification?.body,
data: remoteMessage.data, // optional data payload
};
// Schedule the notification with a null trigger to show immediately
await Notifications.scheduleNotificationAsync({
content: notification,
trigger: null,
});
};
messaging().onMessage(handlePushNotification);
}
loadNotification();
});
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
return (
<View style={styles.container}>
<Text>Open up App.tsx to start working on your app!</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});