Skip to content

Commit c1bb089

Browse files
committed
update
1 parent 96abdd6 commit c1bb089

5 files changed

Lines changed: 49 additions & 42 deletions

File tree

apps/mobile/src/initialize/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { initBackgroundFetch } from "./background"
88
import { initCrashlytics } from "./crashlytics"
99
import { initializeDayjs } from "./dayjs"
1010
import { hydrateDatabaseToStore, hydrateQueryClient, hydrateSettings } from "./hydrate"
11+
import { initMessaging } from "./messaging"
1112
import { migrateDatabase } from "./migration"
1213
import { initializePlayer } from "./player"
1314

@@ -41,6 +42,7 @@ export const initializeApp = async () => {
4142
using_indexed_db: true,
4243
})
4344
initCrashlytics()
45+
initMessaging()
4446
initBackgroundFetch()
4547
console.log(`Initialize done,`, `${loadingTime}ms`)
4648
}
Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,24 @@
11
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"
52

63
import { apiClient } from "../lib/api-fetch"
7-
import { toast } from "../lib/toast"
4+
import { requestNotificationPermission } from "../lib/permission"
85
import { whoami } from "../store/user/getters"
96

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-
407
export const initMessaging = () => {
41-
const user = whoami()
42-
if (!user) {
43-
return
44-
}
458
messaging()
469
.getToken()
4710
.then((token) => {
11+
const user = whoami()
12+
if (!user) {
13+
return
14+
}
4815
apiClient.messaging.$post({
4916
json: {
5017
token,
5118
channel: "mobile",
5219
},
5320
})
5421
})
55-
registerForPushNotificationsAsync()
22+
23+
requestNotificationPermission()
5624
}

apps/mobile/src/lib/permission.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as Notifications from "expo-notifications"
2+
import { Platform } from "react-native"
3+
4+
import { toast } from "./toast"
5+
6+
export async function requestNotificationPermission() {
7+
if (Platform.OS === "android") {
8+
Notifications.setNotificationChannelAsync("default", {
9+
name: "default",
10+
importance: Notifications.AndroidImportance.MAX,
11+
vibrationPattern: [0, 250, 250, 250],
12+
lightColor: "#FF231F7C",
13+
})
14+
}
15+
16+
const { status: existingStatus } = await Notifications.getPermissionsAsync()
17+
let finalStatus = existingStatus
18+
if (existingStatus !== "granted") {
19+
const { status } = await Notifications.requestPermissionsAsync()
20+
finalStatus = status
21+
}
22+
if (finalStatus !== "granted") {
23+
toast.error("Permission not granted for notification!")
24+
return false
25+
}
26+
return true
27+
}
28+
29+
export async function setBadgeCountAsyncWithPermission(badgeCount: number) {
30+
const permissionGranted = await requestNotificationPermission()
31+
if (!permissionGranted) {
32+
return false
33+
}
34+
return await Notifications.setBadgeCountAsync(badgeCount)
35+
}

apps/mobile/src/store/unread/hooks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { FeedViewType } from "@follow/constants"
22
import { useMutation, useQuery } from "@tanstack/react-query"
3-
import * as Notifications from "expo-notifications"
43
import { useCallback, useEffect } from "react"
54

5+
import { setBadgeCountAsyncWithPermission } from "@/src/lib/permission"
6+
67
import { useListFeedIds } from "../list/hooks"
78
import { useSubscriptionByView } from "../subscription/hooks"
89
import { unreadSyncService, useUnreadStore } from "./store"
@@ -27,7 +28,7 @@ export const useAutoMarkAsRead = (entryId: string) => {
2728
export function useUnreadCountBadge() {
2829
const unreadCount = useUnreadCounts()
2930
useEffect(() => {
30-
Notifications.setBadgeCountAsync(unreadCount)
31+
setBadgeCountAsyncWithPermission(unreadCount)
3132
}, [unreadCount])
3233
}
3334

apps/mobile/src/store/unread/store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Notifications from "expo-notifications"
33

44
import type { UnreadSchema } from "@/src/database/schemas/types"
55
import { apiClient } from "@/src/lib/api-fetch"
6+
import { setBadgeCountAsyncWithPermission } from "@/src/lib/permission"
67
import { EntryService } from "@/src/services/entry"
78
import { UnreadService } from "@/src/services/unread"
89

@@ -39,7 +40,7 @@ class UnreadSyncService {
3940
if (allUnreadCount === currentBadgeCount) {
4041
return false
4142
}
42-
await Notifications.setBadgeCountAsync(allUnreadCount)
43+
setBadgeCountAsyncWithPermission(allUnreadCount)
4344
return true
4445
}
4546

0 commit comments

Comments
 (0)