|
1 | 1 | import messaging from "@react-native-firebase/messaging" |
2 | | -import * as Device from "expo-device" |
3 | | -import * as Notifications from "expo-notifications" |
4 | | -import { Platform } from "react-native" |
5 | 2 |
|
6 | 3 | import { apiClient } from "../lib/api-fetch" |
7 | | -import { toast } from "../lib/toast" |
| 4 | +import { requestNotificationPermission } from "../lib/permission" |
8 | 5 | import { whoami } from "../store/user/getters" |
9 | 6 |
|
10 | | -function handleRegistrationError(message: string) { |
11 | | - toast.error(message) |
12 | | -} |
13 | | - |
14 | | -async function registerForPushNotificationsAsync() { |
15 | | - if (Platform.OS === "android") { |
16 | | - Notifications.setNotificationChannelAsync("default", { |
17 | | - name: "default", |
18 | | - importance: Notifications.AndroidImportance.MAX, |
19 | | - vibrationPattern: [0, 250, 250, 250], |
20 | | - lightColor: "#FF231F7C", |
21 | | - }) |
22 | | - } |
23 | | - |
24 | | - if (Device.isDevice) { |
25 | | - const { status: existingStatus } = await Notifications.getPermissionsAsync() |
26 | | - let finalStatus = existingStatus |
27 | | - if (existingStatus !== "granted") { |
28 | | - const { status } = await Notifications.requestPermissionsAsync() |
29 | | - finalStatus = status |
30 | | - } |
31 | | - if (finalStatus !== "granted") { |
32 | | - handleRegistrationError("Permission not granted to get push token for push notification!") |
33 | | - return |
34 | | - } |
35 | | - } else { |
36 | | - handleRegistrationError("Must use physical device for push notifications") |
37 | | - } |
38 | | -} |
39 | | - |
40 | 7 | export const initMessaging = () => { |
41 | | - const user = whoami() |
42 | | - if (!user) { |
43 | | - return |
44 | | - } |
45 | 8 | messaging() |
46 | 9 | .getToken() |
47 | 10 | .then((token) => { |
| 11 | + const user = whoami() |
| 12 | + if (!user) { |
| 13 | + return |
| 14 | + } |
48 | 15 | apiClient.messaging.$post({ |
49 | 16 | json: { |
50 | 17 | token, |
51 | 18 | channel: "mobile", |
52 | 19 | }, |
53 | 20 | }) |
54 | 21 | }) |
55 | | - registerForPushNotificationsAsync() |
| 22 | + |
| 23 | + requestNotificationPermission() |
56 | 24 | } |
0 commit comments