通知機能の仮実装
This commit is contained in:
parent
ab0805689f
commit
d1d94ef307
78
App.js
78
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 (
|
||||
<SafeAreaProvider>
|
||||
<GestureHandlerRootView style={{ flex: 1 }}>
|
||||
@ -54,6 +125,9 @@ export default function App() {
|
||||
<AllTrainDiagramProvider>
|
||||
<BusAndTrainDataProvider>
|
||||
<SheetProvider>
|
||||
<Text onPress={() => Clipboard.setString(expoPushToken)}>
|
||||
{expoPushToken}
|
||||
</Text>
|
||||
<AppContainer />
|
||||
</SheetProvider>
|
||||
</BusAndTrainDataProvider>
|
||||
|
Loading…
Reference in New Issue
Block a user