import { Data } from '../../../../modules/Data/Data' ; import { Api } from '../../../../modules/Api/Api' ; import { Logger } from '../../../../logger' ; import * as CONSTANTS from '../../../../constants' ; import { IPushService , IPushServiceSubscriptionKeys , } from '../../PushService.types' ; import { IPushServiceSafariConfig } from './PushServiceSafari.types' ; export class PushServiceSafari implements IPushService { private readonly api : Api ; private readonly data : Data ; private readonly config : IPushServiceSafariConfig ; constructor ( api , data , config ) { this . api = api ; this . config = config ; this . data = data ; } public getPermission ( ) { const { permission } = this . getPermissionInfo ( ) ; return permission ; } public checkIsPermissionGranted ( ) { return this . getPermission ( ) === CONSTANTS . PERMISSION_GRANTED ; } public checkIsPermissionDefault ( ) { return this . getPermission ( ) === CONSTANTS . PERMISSION_PROMPT ; } public async checkIsManualUnsubscribed ( ) { return await this . data . getStatusManualUnsubscribed ( ) ; } public async askPermission ( ) { const application = await this . data . getApplicationCode ( ) ; const hwid = await this . data . getHwid ( ) ; const payload = { application , hwid , } ; return new Promise ( ( resolve ) => { safari . pushNotification . requestPermission ( this . config . entrypoint || 'https://cp.pushwoosh.com/json/1.3/safari' , this . config . webSitePushId , payload , ( ) => resolve ( ) ) ; } ) ; } public async getTokens ( ) { return this . data . getTokens ( ) ; } public async subscribe ( ) { const isPermissionGranted = this . checkIsPermissionGranted ( ) ; if ( ! isPermissionGranted ) { Logger . error ( 'You must have permission granted before subscribe!' ) ; return ; } const { deviceToken } = await this . getPermissionInfo ( ) ; await this . data . setTokens ( { pushToken : deviceToken } ) ; await this . api . registerDevice ( ) ; } public async unsubscribe ( ) { await this . data . setTokens ( { } ) ; await this . data . setStatusManualUnsubscribed ( true ) ; await this . api . unregisterDevice ( ) ; } public async checkIsRegister ( ) { return this . api . checkDeviceSubscribeForPushNotifications ( ) ; } public async checkIsNeedResubscribe ( ) { const savedWebSitePushId = await this . data . getWebSitePushId ( ) ; const isExistSavedWebSitePushId = typeof savedWebSitePushId !== 'undefined' ; const isChangeWebSitePushId = isExistSavedWebSitePushId && this . config . webSitePushId !== savedWebSitePushId ; await this . data . setWebSitePushId ( this . config . webSitePushId ) ; const lastPermission = await this . data . getLastPermissionStatus ( ) ; const permission = this . getPermission ( ) ; if ( lastPermission !== permission ) { await this . data . setLastPermissionStatus ( permission ) ; return true ; } return isChangeWebSitePushId ; } private getPermissionInfo ( ) { return safari . pushNotification . permission ( this . config . webSitePushId ) ; } }