UNPKG

@talkjs/react-native

Version:

Official TalkJS SDK for React Native

133 lines (132 loc) 4.93 kB
"use strict"; import { Platform } from 'react-native'; import AsyncStorage from '@react-native-async-storage/async-storage'; export let AuthorizationStatus = /*#__PURE__*/function (AuthorizationStatus) { AuthorizationStatus[AuthorizationStatus["notDetermined"] = 0] = "notDetermined"; // The user hasn't yet made a choice about whether the app is allowed to schedule notifications. AuthorizationStatus[AuthorizationStatus["denied"] = 1] = "denied"; // The app isn't authorized to schedule or receive notifications. AuthorizationStatus[AuthorizationStatus["authorized"] = 2] = "authorized"; // The app is authorized to schedule or receive notifications. AuthorizationStatus[AuthorizationStatus["provisional"] = 3] = "provisional"; // The application is provisionally authorized to post noninterruptive user notifications. AuthorizationStatus[AuthorizationStatus["ephemeral"] = 4] = "ephemeral"; // The app is authorized to schedule or receive notifications for a limited amount of time. return AuthorizationStatus; }({}); export let AndroidVisibility = /*#__PURE__*/function (AndroidVisibility) { /** * Show the notification on all lockscreens, but conceal sensitive or private information on secure lockscreens. */ AndroidVisibility[AndroidVisibility["PRIVATE"] = 0] = "PRIVATE"; /** * Show this notification in its entirety on all lockscreens. */ AndroidVisibility[AndroidVisibility["PUBLIC"] = 1] = "PUBLIC"; /** * Do not reveal any part of this notification on a secure lockscreen. * * Useful for notifications showing sensitive information such as banking apps. */ AndroidVisibility[AndroidVisibility["SECRET"] = -1] = "SECRET"; return AndroidVisibility; }({}); export let AndroidImportance = /*#__PURE__*/function (AndroidImportance) { AndroidImportance[AndroidImportance["HIGH"] = 4] = "HIGH"; AndroidImportance[AndroidImportance["DEFAULT"] = 3] = "DEFAULT"; AndroidImportance[AndroidImportance["LOW"] = 2] = "LOW"; AndroidImportance[AndroidImportance["MIN"] = 1] = "MIN"; AndroidImportance[AndroidImportance["NONE"] = 0] = "NONE"; return AndroidImportance; }({}); export class TokenHandler { #deviceToken; #storageKey = 'TalkJS_push_token'; #tokenPromise; #onTokenRefresh; #onNotificationPressed; constructor(provider) { this.#deviceToken = { provider, pushRegistrationId: '' }; this.#tokenPromise = new Promise(resolve => this.resolveTokenPromise = resolve); AsyncStorage.getItem(this.#storageKey).then(value => { if (value && this.#deviceToken.pushRegistrationId === '') { this.#deviceToken.pushRegistrationId = value; } }); } get deviceToken() { return this.#deviceToken; } _getTokenPromise() { return this.#tokenPromise; } onTokenRefresh(handler) { this.#onTokenRefresh = handler; } onNotificationPressed(handler) { this.#onNotificationPressed = handler; } notificationPressed(event, isForegroundEvent) { if (this.#onNotificationPressed) { const result = { isForegroundEvent, id: '' }; let talkjsData = undefined; if (event.detail) { const notification = event.detail.notification; result.id = notification.id ?? ''; if (notification.data?.talkjs) { if (Platform.OS === 'android') { talkjsData = JSON.parse(notification.data.talkjs); } else { talkjsData = notification.data.talkjs; } } } else { // Is Firebase Message const notification = event; result.id = notification.messageId ?? ''; if (notification.data?.talkjs) { if (Platform.OS === 'android') { talkjsData = JSON.parse(notification.data.talkjs); } else { talkjsData = notification.data.talkjs; } } } if (talkjsData !== undefined) { result.body = talkjsData.body; result.title = talkjsData.sender.name; result.data = { senderId: talkjsData.sender.id, messageId: talkjsData.message.id, conversationId: talkjsData.conversation.id, timestamp: talkjsData.timestamp }; } this.#onNotificationPressed(result); } } setPushRegistrationId(value) { if (this.#deviceToken.pushRegistrationId !== value) { if (this.#onTokenRefresh) { const oldToken = { provider: this.#deviceToken.provider, pushRegistrationId: this.#deviceToken.pushRegistrationId }; const newToken = { provider: this.#deviceToken.provider, pushRegistrationId: value }; this.#onTokenRefresh(oldToken, newToken); } this.#deviceToken.pushRegistrationId = value; AsyncStorage.setItem(this.#storageKey, value); } this.resolveTokenPromise(); } } //# sourceMappingURL=Notification.js.map