Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Nov 22, 2024
1 parent 15531e3 commit b24f0f8
Showing 1 changed file with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import messaging from '@react-native-firebase/messaging'
import {isRecord, isString} from '@yoroi/common'
import {createTypeGuardFromSchema, isKeyOf} from '@yoroi/common/src'
import React from 'react'
import {PermissionsAndroid} from 'react-native'
import {NotificationBackgroundFetchResult, Notifications} from 'react-native-notifications'

Check failure on line 4 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

'NotificationBackgroundFetchResult' is defined but never used. Allowed unused vars must match /^_/u
import {z} from 'zod'

import {notificationManager} from './notification-manager'
import {parseNotificationId, sendNotification} from './notifications'
import {generateNotificationId, parseNotificationId, sendNotification} from './notifications'
import {usePrimaryTokenPriceChangedNotification} from './primary-token-price-changed-notification'
import {useRewardsUpdatedNotifications} from './rewards-updated-notification'
import {useTransactionReceivedNotifications} from './transaction-received-notification'
Expand All @@ -18,25 +15,35 @@ const init = () => {
if (initialized) return
initialized = true
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
Notifications.events().registerNotificationReceivedForeground((_notification, completion) => {
completion({alert: true, sound: true, badge: true})
})

Notifications.events().registerNotificationReceivedBackground((_notification, completion) => {
completion(NotificationBackgroundFetchResult.NEW_DATA)
const unsubscribeFromForegroundMessage = messaging().onMessage((remoteMessage) => {
const {notification} = remoteMessage
if (notification && notification.title && notification.body) {
sendNotification({
title: notification.title,
body: notification.body,
id: generateNotificationId(),
})
// TODO: Save notification to local storage
console.log('FCM Message Notification:', remoteMessage.notification)
}
})

Notifications.events().registerNotificationOpened((notification, completion) => {
const payloadId = notification.identifier || notification.payload.id
const id = parseNotificationId(payloadId)
notificationManager.events.markAsRead(id)
completion()
})
const notificationOpenedSubscription = Notifications.events().registerNotificationOpened(
(notification, completion) => {
const payloadId = notification.identifier || notification.payload.id
const id = parseNotificationId(payloadId)
notificationManager.events.markAsRead(id)
completion()
},
)

notificationManager.hydrate()

return () => {
notificationManager.destroy()
notificationOpenedSubscription.remove()
unsubscribeFromForegroundMessage()
}
}

Expand All @@ -55,24 +62,9 @@ const usePushNotifications = ({enabled}: {enabled: boolean}) => {
}, [enabled])
}

messaging().setBackgroundMessageHandler((remoteMessage) => {
const data = remoteMessage.data as unknown
if (!isRecord(data) || !isKeyOf('custom', data) || !isString(data.custom)) return Promise.resolve()

try {
const custom = JSON.parse(data.custom) as unknown
if (!isValidNotificationData(custom)) return Promise.resolve()
sendNotification({title: custom.title, id: 123, body: custom.body})
} catch (e) {
console.error(e)
messaging().setBackgroundMessageHandler(async (remoteMessage) => {

Check failure on line 65 in apps/wallet-mobile/src/features/Notifications/useCases/common/hooks.ts

View workflow job for this annotation

GitHub Actions / check

Async arrow function has no 'await' expression
if (remoteMessage.notification) {
// Automatically shown by the OS
console.log('FCM Message Notification in background:', remoteMessage.notification)
}

return Promise.resolve()
})

const isValidNotificationData = createTypeGuardFromSchema(
z.object({
title: z.string(),
body: z.string(),
}),
)

0 comments on commit b24f0f8

Please sign in to comment.