Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for new notifications #2273

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
107 changes: 50 additions & 57 deletions src/push.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<void> {
Expand All @@ -93,5 +86,5 @@ export default {

unregister(): Promise<string> {
return fetchText('/mobile/unregister', { method: 'POST' })
}
},
}