MCPcopy Create free account
hub / github.com/academind/react-native-practical-guide-code / App

Function App

code/15-notifications/03-finished/App.js:16–124  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14});
15
16export default function App() {
17 useEffect(() => {
18 async function configurePushNotifications() {
19 const { status } = await Notifications.getPermissionsAsync();
20 let finalStatus = status;
21
22 if (finalStatus !== 'granted') {
23 const { status } = await Notifications.requestPermissionsAsync();
24 finalStatus = status;
25 }
26
27 if (finalStatus !== 'granted') {
28 Alert.alert(
29 'Permission required',
30 'Push notifications need the appropriate permissions.'
31 );
32 return;
33 }
34
35 const pushTokenData = await Notifications.getExpoPushTokenAsync();
36 console.log(pushTokenData);
37
38 if (Platform.OS === 'android') {
39 Notifications.setNotificationChannelAsync('default', {
40 name: 'default',
41 importance: Notifications.AndroidImportance.DEFAULT
42 });
43 }
44 }
45
46 configurePushNotifications();
47 }, []);
48
49 useEffect(() => {
50 const subscription1 = Notifications.addNotificationReceivedListener(
51 (notification) => {
52 console.log('NOTIFICATION RECEIVED');
53 console.log(notification);
54 const userName = notification.request.content.data.userName;
55 console.log(userName);
56 }
57 );
58
59 const subscription2 = Notifications.addNotificationResponseReceivedListener(
60 (response) => {
61 console.log('NOTIFICATION RESPONSE RECEIVED');
62 console.log(response);
63 const userName = response.notification.request.content.data.userName;
64 console.log(userName);
65 }
66 );
67
68 return () => {
69 subscription1.remove();
70 subscription2.remove();
71 };
72 }, []);
73

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected