Skip to content

Commit

Permalink
Improve parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Nov 21, 2024
1 parent cef7dcb commit 15531e3
Showing 1 changed file with 26 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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 {Notifications, Registered, RegistrationError} from 'react-native-notifications'
import {NotificationBackgroundFetchResult} from 'react-native-notifications'
import messaging from '@react-native-firebase/messaging'
import {NotificationBackgroundFetchResult, Notifications} from 'react-native-notifications'
import {z} from 'zod'

import {notificationManager} from './notification-manager'
import {displayNotificationEvent, parseNotificationId, sendNotification} from './notifications'
import {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 @@ -17,12 +19,10 @@ const init = () => {
initialized = true
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS)
Notifications.events().registerNotificationReceivedForeground((_notification, completion) => {
console.log('Notification received in foreground')
completion({alert: true, sound: true, badge: true})
})

Notifications.events().registerNotificationReceivedBackground((_notification, completion) => {
console.log('Notification received in background')
completion(NotificationBackgroundFetchResult.NEW_DATA)
})

Expand All @@ -49,43 +49,30 @@ export const useInitNotifications = ({enabled}: {enabled: boolean}) => {
}

const usePushNotifications = ({enabled}: {enabled: boolean}) => {
console.log('usePushNotifications', enabled)
React.useEffect(() => {
if (!enabled) return
// Notifications.getInitialNotification().then((notification) => {
// console.log('Initial notification', notification)
// })
//
// Notifications.events().registerRemoteNotificationsRegistrationDenied(() => {
// console.log('NOTIFICATION PUSH: User denied remote notifications')
// })
// Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
// // TODO: Send the token to my server so it could send back push notifications...
// console.log('NOTIFICATION PUSH: Device Token Received', event.deviceToken)
// })
// Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
// console.log('NOTIFICATION PUSH: Failed to register for remote notifications', event)
// })
Notifications.registerRemoteNotifications({})
const s1 = messaging().onMessage(async (remoteMessage) => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage))
})

messaging()
.getToken()
.then((token) => {
console.log('FCM Token:', token)
})

return () => {
s1()
}
}, [enabled])
}

messaging().setBackgroundMessageHandler(async function (remoteMessage) {
// self.registration.showNotification('Title', {body: 'Body', icon: '/icon.png'})
console.log('Message handled in the background!', JSON.stringify(remoteMessage), (self as any).registration)
sendNotification({title: 'tiiiitle', id: 123, body: 'boooody'})
// self.registration.showNotification('Title', {body: 'Body', icon: '/icon.png'});
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)
}

return Promise.resolve()
})

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

0 comments on commit 15531e3

Please sign in to comment.