web notifications
This commit is contained in:
@@ -9,8 +9,10 @@ import React, {
|
||||
useRef,
|
||||
useState,
|
||||
} from "react";
|
||||
import { Platform } from "react-native";
|
||||
import { useGlobal } from "reactn";
|
||||
import firebase, {
|
||||
arrayUnion,
|
||||
notificationsRef,
|
||||
serverTimestamp,
|
||||
usersRef,
|
||||
@@ -152,35 +154,52 @@ export default function NotificationProvider({ children }) {
|
||||
|
||||
async function registerForPushNotificationsAsync() {
|
||||
try {
|
||||
if (Device.isDevice) {
|
||||
let { status } = await Notifications.getPermissionsAsync();
|
||||
if (status !== "granted") {
|
||||
const request = await Notifications.requestPermissionsAsync();
|
||||
status = request.status;
|
||||
}
|
||||
if (status !== "granted") {
|
||||
console.log("Notification permissions denied");
|
||||
setAllowNotifications(false);
|
||||
return;
|
||||
}
|
||||
setAllowNotifications(true);
|
||||
|
||||
// Get the push token
|
||||
const pushToken = (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
projectId: Constants.expoConfig?.extra?.eas?.projectId || "",
|
||||
})
|
||||
).data;
|
||||
if (user?.pushToken !== pushToken && !!user) {
|
||||
await usersRef.doc(uid).update({ pushToken });
|
||||
// if (!user?.alerts) {
|
||||
// await usersRef.doc(uid).update({
|
||||
// alerts: Object.values(ALERT_TYPE),
|
||||
// });
|
||||
// }
|
||||
}
|
||||
} else {
|
||||
const isWeb = Platform.OS === "web";
|
||||
if (!Device.isDevice && !isWeb) {
|
||||
console.log("Must use physical device for Push Notifications");
|
||||
setAllowNotifications(false);
|
||||
setNotifInit(true);
|
||||
return;
|
||||
}
|
||||
|
||||
let { status } = await Notifications.getPermissionsAsync();
|
||||
if (status !== "granted") {
|
||||
const request = await Notifications.requestPermissionsAsync();
|
||||
status = request.status;
|
||||
}
|
||||
|
||||
if (status !== "granted") {
|
||||
console.log("Notification permissions denied");
|
||||
setAllowNotifications(false);
|
||||
return;
|
||||
}
|
||||
|
||||
setAllowNotifications(true);
|
||||
|
||||
const projectId =
|
||||
Constants.expoConfig?.extra?.eas?.projectId ||
|
||||
Constants.manifest2?.extra?.eas?.projectId ||
|
||||
Constants.manifest?.extra?.eas?.projectId ||
|
||||
"";
|
||||
|
||||
const pushToken = (
|
||||
await Notifications.getExpoPushTokenAsync({
|
||||
projectId,
|
||||
})
|
||||
)?.data;
|
||||
|
||||
if (!pushToken) {
|
||||
console.log("No push token retrieved");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!!user && pushToken) {
|
||||
await usersRef.doc(uid).set(
|
||||
{
|
||||
pushTokens: arrayUnion(pushToken),
|
||||
},
|
||||
{ merge: true }
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
|
||||
Reference in New Issue
Block a user