From b2d11ce5fa640bb1747945dcae039c570a71e348 Mon Sep 17 00:00:00 2001 From: KamilKozakowski Date: Thu, 20 Oct 2022 15:29:49 +0200 Subject: [PATCH] notifications --- App.js | 55 +- app.json | 5 + eas.json | 18 + notes.md | 10 +- package-lock.json | 3987 +++++++++++---------------------------------- package.json | 2 + 6 files changed, 1081 insertions(+), 2996 deletions(-) create mode 100644 eas.json diff --git a/App.js b/App.js index b64a975..2529c35 100644 --- a/App.js +++ b/App.js @@ -1,11 +1,63 @@ import { StatusBar } from 'expo-status-bar'; -import { useState, useRef } from 'react'; +import { useState, useRef, useEffect } from 'react'; import { StyleSheet, Text, View, Button } from 'react-native'; +import * as Device from 'expo-device'; +import * as Notifications from 'expo-notifications'; import Modal from "react-native-modal"; + + export default function App() { const [modalVisible, setModalVisible] = useState(false); + const [expoPushToken, setExpoPushToken] = useState(); + const [notification, setNotification] = useState(); + useEffect(()=>{ + registerForPushNotificationsAsync().catch((error) => { + console.log('There has been a problem with your fetch operation: ' + error.message); + // ADD THIS THROW error + throw error; + }); + Notifications.setNotificationHandler({ + handleNotification: async () => ({ + shouldShowAlert: true, + shouldPlaySound: false, + shouldSetBadge: false, + }), + }); + Notifications.addNotificationReceivedListener(() => setNotification(notification)); + Notifications.addNotificationResponseReceivedListener((response) => console.log(response)); + },[]); + + registerForPushNotificationsAsync = async () => { + if (Device.isDevice) { + const { status: existingStatus } = await Notifications.getPermissionsAsync(); + let finalStatus = existingStatus; + if (existingStatus !== 'granted') { + const { status } = await Notifications.requestPermissionsAsync(); + finalStatus = status; + } + if (finalStatus !== 'granted') { + alert('Failed to get push token for push notification!'); + return; + } else { + console.log(finalStatus) + } + const token = (await Notifications.getExpoPushTokenAsync()).data; + console.log(token); + setExpoPushToken(token); + } else { + alert('Must use physical device for Push Notifications'); + } + + // if (Platform.OS === 'android') { + // Notifications.setNotificationChannelAsync('default', { + // name: 'default', + // importance: Notifications.AndroidImportance.MAX, + // vibrationPattern: [0, 250, 250, 250], + // lightColor: '#FF231F7C', + // });} + }; return ( <> {/* SWIPEABLE MODAL START */} @@ -38,6 +90,7 @@ export default function App() {