diff --git a/src/push.ts b/src/push.ts index 91e15cc66..4d1264b08 100644 --- a/src/push.ts +++ b/src/push.ts @@ -1,11 +1,7 @@ -import { - PushNotifications, - PushNotificationSchema, - Token, - ActionPerformed -} from '@capacitor/push-notifications' +import { PushNotifications, PushNotificationSchema, Token, ActionPerformed } from '@capacitor/push-notifications' import { fetchText } from './http' import challengesApi from './lichess/challenges' +import { openExternalBrowser } from './utils/browse' import router from './router' import session from './session' import settings from './settings' @@ -16,65 +12,62 @@ export default { isStub: false, init() { - PushNotifications.addListener('registration', - ({ value }: Token) => { - console.debug('Push registration success, FCM token: ' + value) + PushNotifications.addListener('registration', ({ value }: Token) => { + console.debug('Push registration success, FCM token: ' + value) - fetchText(`/mobile/register/firebase/${value}`, { - method: 'POST' - }) - .catch(handleXhrError) - } - ) + fetchText(`/mobile/register/${value}`, { + method: 'POST', + }).catch(handleXhrError) + }) - PushNotifications.addListener('registrationError', - (error: any) => { - console.error('Error on registration: ' + JSON.stringify(error)) - } - ) + PushNotifications.addListener('registrationError', (error: any) => { + console.error('Error on registration: ' + JSON.stringify(error)) + }) - PushNotifications.addListener('pushNotificationReceived', - (notification: PushNotificationSchema) => { - if (isForeground()) { - switch (notification.data['lichess.type']) { - case 'corresAlarm': - case 'gameTakebackOffer': - case 'gameDrawOffer': - case 'challengeAccept': - case 'gameMove': - case 'gameFinish': - session.refresh() - break - } + PushNotifications.addListener('pushNotificationReceived', (notification: PushNotificationSchema) => { + if (isForeground()) { + switch (notification.data['lichess.type']) { + case 'corresAlarm': + case 'gameTakebackOffer': + case 'gameDrawOffer': + case 'challengeAccept': + case 'gameMove': + case 'gameFinish': + session.refresh() + break } } - ) + }) - PushNotifications.addListener('pushNotificationActionPerformed', - (action: ActionPerformed) => { - if (action.actionId === 'tap') { - switch (action.notification.data['lichess.type']) { - case 'challengeAccept': - challengesApi.refresh() - router.set(`/game/${action.notification.data['lichess.challengeId']}`) - break - case 'challengeCreate': - router.set(`/game/${action.notification.data['lichess.challengeId']}`) - break - case 'corresAlarm': - case 'gameMove': - case 'gameFinish': - case 'gameTakebackOffer': - case 'gameDrawOffer': - router.set(`/game/${action.notification.data['lichess.fullId']}`) - break - case 'newMessage': - router.set(`/inbox/${action.notification.data['lichess.threadId']}`) - break + PushNotifications.addListener('pushNotificationActionPerformed', (action: ActionPerformed) => { + if (action.actionId === 'tap') { + switch (action.notification.data['lichess.type']) { + case 'challengeAccept': + challengesApi.refresh() + router.set(`/game/${action.notification.data['lichess.challengeId']}`) + break + case 'challengeCreate': + router.set(`/game/${action.notification.data['lichess.challengeId']}`) + break + case 'corresAlarm': + case 'gameMove': + case 'gameFinish': + case 'gameTakebackOffer': + case 'gameDrawOffer': + router.set(`/game/${action.notification.data['lichess.fullId']}`) + break + case 'newMessage': + router.set(`/inbox/${action.notification.data['lichess.threadId']}`) + break + default: { + const url = action.notification.data['lichess.url'] as string + if (url?.startsWith('https://lichess.org/')) { + void openExternalBrowser(url) + } } } } - ) + }) }, async register(): Promise { @@ -93,5 +86,5 @@ export default { unregister(): Promise { return fetchText('/mobile/unregister', { method: 'POST' }) - } + }, }