mirror of
https://github.com/CompassConnections/Compass.git
synced 2026-02-24 19:06:37 -05:00
* Test * Add pretty formatting * Fix Tests * Fix Tests * Fix Tests * Fix * Add pretty formatting fix * Fix * Test * Fix tests * Clean typeckech * Add prettier check * Fix api tsconfig * Fix api tsconfig * Fix tsconfig * Fix * Fix * Prettier
49 lines
1.4 KiB
TypeScript
49 lines
1.4 KiB
TypeScript
import {PushNotifications} from '@capacitor/push-notifications'
|
|
import {useRouter} from 'next/router'
|
|
import {useEffect} from 'react'
|
|
import {useUser} from 'web/hooks/use-user'
|
|
import {api} from 'web/lib/api'
|
|
import {isAndroidApp} from 'web/lib/util/webview'
|
|
|
|
export default function AndroidPush() {
|
|
const user = useUser() // authenticated user
|
|
const isAndroid = isAndroidApp()
|
|
const router = useRouter()
|
|
useEffect(() => {
|
|
if (!user?.id || !isAndroid) return
|
|
console.log('AndroidPush', user)
|
|
|
|
PushNotifications.requestPermissions().then((result) => {
|
|
if (result.receive !== 'granted') {
|
|
console.log('Push notifications not granted')
|
|
return
|
|
}
|
|
PushNotifications.register()
|
|
console.log('Push registered')
|
|
})
|
|
|
|
PushNotifications.addListener('registration', async (token) => {
|
|
console.log('Device token:', token.value)
|
|
try {
|
|
const {data} = await api('save-subscription-mobile', {
|
|
token: token.value,
|
|
})
|
|
console.log('Mobile subscription saved:', data)
|
|
} catch (err) {
|
|
console.error('Failed saving android subscription', err)
|
|
}
|
|
})
|
|
|
|
PushNotifications.addListener('pushNotificationReceived', (notif) => {
|
|
console.log('Push received', notif)
|
|
const url = notif?.data?.url
|
|
if (url) {
|
|
router.push(url)
|
|
window.location.href = url
|
|
}
|
|
})
|
|
}, [user?.id, isAndroid])
|
|
|
|
return null
|
|
}
|