-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpoPushNotifications.js
More file actions
46 lines (42 loc) · 1.52 KB
/
expoPushNotifications.js
File metadata and controls
46 lines (42 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
import Constants from 'expo-constants';
import { auth, db } from './firebaseConfig';
import { doc, updateDoc } from 'firebase/firestore';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
export async function registerForPushNotificationsAsync() {
let token;
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})).data;
// console.log("Expo push token:", 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',
});
}
// await updateDoc(doc(db, "users", auth.currentUser.uid), { notifToken: notifToken }, { merge: true });
return token;
}