From d1d94ef307a443a9718440845f243e16fefb8de4 Mon Sep 17 00:00:00 2001 From: harukin-OneMix4 Date: Fri, 26 Jan 2024 21:50:39 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=9F=A5=E6=A9=9F=E8=83=BD=E3=81=AE?= =?UTF-8?q?=E4=BB=AE=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/App.js b/App.js index da40942..8d14225 100644 --- a/App.js +++ b/App.js @@ -1,9 +1,12 @@ -import React, { useEffect } from "react"; +import React, { useEffect, useState, useRef } from "react"; import { NavigationContainer } from "@react-navigation/native"; import { createStackNavigator } from "@react-navigation/stack"; import { createBottomTabNavigator } from "@react-navigation/bottom-tabs"; -import { Platform, UIManager } from "react-native"; +import { Platform, UIManager, Text, Clipboard } from "react-native"; import { GestureHandlerRootView } from "react-native-gesture-handler"; +import * as Device from "expo-device"; +import * as Notifications from "expo-notifications"; +import Constants from "expo-constants"; import { UpdateAsync } from "./UpdateAsync.js"; import { AS } from "./storageControl"; import TNDView from "./ndView"; @@ -41,9 +44,77 @@ if (Platform.OS === "android") { UIManager.setLayoutAnimationEnabledExperimental(true); } } +Notifications.setNotificationHandler({ + handleNotification: async () => ({ + shouldShowAlert: true, + shouldPlaySound: false, + shouldSetBadge: false, + }), +}); +async function registerForPushNotificationsAsync() { + let token; + if (Platform.OS === "android") { + Notifications.setNotificationChannelAsync("default", { + name: "default", + importance: Notifications.AndroidImportance.MAX, + vibrationPattern: [0, 250, 250, 250], + lightColor: "#FF231F7C", + }); + } + + 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; + } + token = await Notifications.getExpoPushTokenAsync({ + projectId: Constants.expoConfig.extra.eas.projectId, + }); + console.log(token); + } else { + alert("Must use physical device for Push Notifications"); + } + + return token.data; +} export default function App() { + const [expoPushToken, setExpoPushToken] = useState(""); + const [notification, setNotification] = useState(false); + const notificationListener = useRef(); + const responseListener = useRef(); + + useEffect(() => { + registerForPushNotificationsAsync().then((token) => + setExpoPushToken(token) + ); + + notificationListener.current = + Notifications.addNotificationReceivedListener((notification) => { + setNotification(notification); + }); + + responseListener.current = + Notifications.addNotificationResponseReceivedListener((response) => { + console.log(response); + }); + + return () => { + Notifications.removeNotificationSubscription( + notificationListener.current + ); + Notifications.removeNotificationSubscription(responseListener.current); + }; + }, []); useEffect(() => UpdateAsync(), []); + return ( @@ -54,6 +125,9 @@ export default function App() { + Clipboard.setString(expoPushToken)}> + {expoPushToken} +