-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushNotificationHelper.js
64 lines (56 loc) · 1.88 KB
/
PushNotificationHelper.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as Notifications from 'expo-notifications';
import { Platform } from 'react-native';
import * as Device from 'expo-device';
import Constants from 'expo-constants';
import axios from 'axios';
import { Base_url } from './Config/BaseUrl';
export async function registerForPushNotificationsAsync(userId) {
let token;
if (Platform.OS === 'android') {
await 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;
}
try {
const projectId = Constants.expoConfig.extra.eas.projectId ?? Constants.easConfig.projectId;
if (!projectId) {
throw new Error('Project ID not found');
}
token = (
await Notifications.getExpoPushTokenAsync({
projectId,
})
).data;
console.log("Expo Push Token:", token);
} catch (e) {
console.error("Error getting push token:", e);
}
} else {
alert('Must use physical device for Push Notifications');
}
if (token) {
try {
const res = await axios.post(`${Base_url}api/users/${userId}/update-token`, { notificationToken: token });
console.log("Response from token update:", res.data);
} catch (error) {
console.error("Error updating token on backend:", error.response ? error.response.data : error.message);
}
} else {
console.error("Failed to obtain token");
}
return token;
}