-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathpush.ts
97 lines (88 loc) · 2.77 KB
/
push.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {
PushNotifications,
PushNotificationSchema,
Token,
ActionPerformed
} from '@capacitor/push-notifications'
import { fetchText } from './http'
import challengesApi from './lichess/challenges'
import router from './router'
import session from './session'
import settings from './settings'
import { handleXhrError } from './utils'
import { isForeground } from './utils/appMode'
export default {
isStub: false,
init() {
PushNotifications.addListener('registration',
({ value }: Token) => {
console.debug('Push registration success, FCM token: ' + value)
fetchText(`/mobile/register/firebase/${value}`, {
method: 'POST'
})
.catch(handleXhrError)
}
)
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('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
}
}
}
)
},
async register(): Promise<void> {
if (settings.general.notifications.enable()) {
PushNotifications.requestPermissions().then(result => {
if (result.receive === 'granted') {
return PushNotifications.register()
} else {
return Promise.reject('Permission to use push denied')
}
})
}
return Promise.resolve()
},
unregister(): Promise<string> {
return fetchText('/mobile/unregister', { method: 'POST' })
}
}