@unvired/react-native-unvired-sdk 0.0.12 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BuildNo.txt +1 -1
- package/README.md +884 -255
- package/dist/database/Database.d.ts +71 -0
- package/dist/database/Database.js +156 -0
- package/dist/database/DatabaseManager.d.ts +12 -0
- package/dist/database/DatabaseManager.js +18 -0
- package/dist/database/index.d.ts +6 -0
- package/dist/database/index.js +5 -0
- package/dist/database/services/DatabaseNative.d.ts +8 -0
- package/dist/database/services/DatabaseNative.js +149 -0
- package/dist/database/services/DatabaseWeb.d.ts +13 -0
- package/dist/database/services/DatabaseWeb.js +138 -0
- package/dist/database/types.d.ts +94 -0
- package/dist/database/types.js +4 -0
- package/dist/device-info/BaseDeviceInfo.d.ts +39 -0
- package/dist/device-info/BaseDeviceInfo.js +116 -0
- package/dist/device-info/DeviceInfo.d.ts +9 -0
- package/dist/device-info/DeviceInfo.js +11 -0
- package/dist/device-info/DeviceInfo.types.d.ts +11 -0
- package/dist/device-info/DeviceInfo.types.js +3 -0
- package/dist/device-info/index.d.ts +3 -0
- package/dist/device-info/index.js +3 -0
- package/dist/file-system/BaseFileSystem.d.ts +61 -0
- package/dist/file-system/BaseFileSystem.js +164 -0
- package/dist/file-system/FileSystem.d.ts +9 -0
- package/dist/file-system/FileSystem.js +11 -0
- package/dist/file-system/FileSystem.types.d.ts +11 -0
- package/dist/file-system/FileSystem.types.js +3 -0
- package/dist/file-system/index.d.ts +3 -0
- package/dist/file-system/index.js +3 -0
- package/dist/file-system/services/FileSystemNative.d.ts +17 -0
- package/dist/file-system/services/FileSystemNative.js +73 -0
- package/dist/file-system/services/FileSystemWeb.d.ts +16 -0
- package/dist/file-system/services/FileSystemWeb.js +41 -0
- package/dist/local-storage/index.d.ts +1 -0
- package/dist/local-storage/index.js +2 -0
- package/dist/local-storage/localStorage.d.ts +49 -0
- package/dist/local-storage/localStorage.js +128 -0
- package/dist/local-storage/services/StorageNative.d.ts +10 -0
- package/dist/local-storage/services/StorageNative.js +27 -0
- package/dist/local-storage/services/StorageWeb.d.ts +10 -0
- package/dist/local-storage/services/StorageWeb.js +26 -0
- package/dist/logger/{Logger.js → index.js} +1 -4
- package/dist/main.d.ts +11 -0
- package/dist/main.js +24 -0
- package/dist/push-notification/BasePushNotification.d.ts +52 -0
- package/dist/push-notification/BasePushNotification.js +180 -0
- package/dist/push-notification/PushNotification.d.ts +9 -0
- package/dist/push-notification/PushNotification.js +11 -0
- package/dist/push-notification/PushNotification.types.d.ts +12 -0
- package/dist/push-notification/PushNotification.types.js +3 -0
- package/dist/push-notification/index.d.ts +3 -0
- package/dist/push-notification/index.js +3 -0
- package/example/USAGE_EXAMPLE.ts +167 -0
- package/package.json +17 -6
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -4
- /package/dist/logger/{Logger.d.ts → index.d.ts} +0 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LocalStorage class provides a wrapper around platform-specific storage
|
|
3
|
+
* - Web: Uses browser localStorage
|
|
4
|
+
* - iOS/Android/Windows: Uses AsyncStorage
|
|
5
|
+
*/
|
|
6
|
+
export declare class LocalStorage {
|
|
7
|
+
/**
|
|
8
|
+
* Get an item from storage
|
|
9
|
+
* @param key - The key to retrieve
|
|
10
|
+
* @returns The parsed value or null if not found
|
|
11
|
+
*/
|
|
12
|
+
static getItem<T = any>(key: string): Promise<T | null>;
|
|
13
|
+
/**
|
|
14
|
+
* Set an item in storage
|
|
15
|
+
* @param key - The key to store the value under
|
|
16
|
+
* @param value - The value to store (will be JSON stringified)
|
|
17
|
+
*/
|
|
18
|
+
static setItem(key: string, value: any): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Remove an item from storage
|
|
21
|
+
* @param key - The key to remove
|
|
22
|
+
*/
|
|
23
|
+
static removeItem(key: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Clear all items from storage
|
|
26
|
+
*/
|
|
27
|
+
static clear(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Get all keys from storage
|
|
30
|
+
* @returns Array of all keys
|
|
31
|
+
*/
|
|
32
|
+
static getAllKeys(): Promise<readonly string[]>;
|
|
33
|
+
/**
|
|
34
|
+
* Get multiple items from storage
|
|
35
|
+
* @param keys - Array of keys to retrieve
|
|
36
|
+
* @returns Array of [key, value] pairs
|
|
37
|
+
*/
|
|
38
|
+
static multiGet(keys: string[]): Promise<Array<[string, any]>>;
|
|
39
|
+
/**
|
|
40
|
+
* Set multiple items in storage
|
|
41
|
+
* @param keyValuePairs - Array of [key, value] pairs to store
|
|
42
|
+
*/
|
|
43
|
+
static multiSet(keyValuePairs: Array<[string, any]>): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Remove multiple items from storage
|
|
46
|
+
* @param keys - Array of keys to remove
|
|
47
|
+
*/
|
|
48
|
+
static multiRemove(keys: string[]): Promise<void>;
|
|
49
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { StorageWeb } from './services/StorageWeb';
|
|
3
|
+
import { StorageNative } from './services/StorageNative';
|
|
4
|
+
// Select storage implementation based on platform
|
|
5
|
+
const storageImpl = Platform.OS === 'web' ? new StorageWeb() : new StorageNative();
|
|
6
|
+
/**
|
|
7
|
+
* LocalStorage class provides a wrapper around platform-specific storage
|
|
8
|
+
* - Web: Uses browser localStorage
|
|
9
|
+
* - iOS/Android/Windows: Uses AsyncStorage
|
|
10
|
+
*/
|
|
11
|
+
export class LocalStorage {
|
|
12
|
+
/**
|
|
13
|
+
* Get an item from storage
|
|
14
|
+
* @param key - The key to retrieve
|
|
15
|
+
* @returns The parsed value or null if not found
|
|
16
|
+
*/
|
|
17
|
+
static async getItem(key) {
|
|
18
|
+
try {
|
|
19
|
+
const value = await storageImpl.getItem(key);
|
|
20
|
+
return value ? JSON.parse(value) : null;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error(`Error getting item with key "${key}":`, error);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Set an item in storage
|
|
29
|
+
* @param key - The key to store the value under
|
|
30
|
+
* @param value - The value to store (will be JSON stringified)
|
|
31
|
+
*/
|
|
32
|
+
static async setItem(key, value) {
|
|
33
|
+
try {
|
|
34
|
+
const jsonValue = JSON.stringify(value);
|
|
35
|
+
await storageImpl.setItem(key, jsonValue);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`Error setting item with key "${key}":`, error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Remove an item from storage
|
|
44
|
+
* @param key - The key to remove
|
|
45
|
+
*/
|
|
46
|
+
static async removeItem(key) {
|
|
47
|
+
try {
|
|
48
|
+
await storageImpl.removeItem(key);
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error(`Error removing item with key "${key}":`, error);
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Clear all items from storage
|
|
57
|
+
*/
|
|
58
|
+
static async clear() {
|
|
59
|
+
try {
|
|
60
|
+
await storageImpl.clear();
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
console.error('Error clearing storage:', error);
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get all keys from storage
|
|
69
|
+
* @returns Array of all keys
|
|
70
|
+
*/
|
|
71
|
+
static async getAllKeys() {
|
|
72
|
+
try {
|
|
73
|
+
return await storageImpl.getAllKeys();
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
console.error('Error getting all keys:', error);
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Get multiple items from storage
|
|
82
|
+
* @param keys - Array of keys to retrieve
|
|
83
|
+
* @returns Array of [key, value] pairs
|
|
84
|
+
*/
|
|
85
|
+
static async multiGet(keys) {
|
|
86
|
+
try {
|
|
87
|
+
const results = await storageImpl.multiGet(keys);
|
|
88
|
+
return results.map(([key, value]) => [
|
|
89
|
+
key,
|
|
90
|
+
value ? JSON.parse(value) : null
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error('Error getting multiple items:', error);
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Set multiple items in storage
|
|
100
|
+
* @param keyValuePairs - Array of [key, value] pairs to store
|
|
101
|
+
*/
|
|
102
|
+
static async multiSet(keyValuePairs) {
|
|
103
|
+
try {
|
|
104
|
+
const jsonPairs = keyValuePairs.map(([key, value]) => [
|
|
105
|
+
key,
|
|
106
|
+
JSON.stringify(value)
|
|
107
|
+
]);
|
|
108
|
+
await storageImpl.multiSet(jsonPairs);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
console.error('Error setting multiple items:', error);
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Remove multiple items from storage
|
|
117
|
+
* @param keys - Array of keys to remove
|
|
118
|
+
*/
|
|
119
|
+
static async multiRemove(keys) {
|
|
120
|
+
try {
|
|
121
|
+
await storageImpl.multiRemove(keys);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.error('Error removing multiple items:', error);
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class StorageNative {
|
|
2
|
+
getItem(key: string): Promise<string | null>;
|
|
3
|
+
setItem(key: string, value: string): Promise<void>;
|
|
4
|
+
removeItem(key: string): Promise<void>;
|
|
5
|
+
clear(): Promise<void>;
|
|
6
|
+
getAllKeys(): Promise<readonly string[]>;
|
|
7
|
+
multiGet(keys: string[]): Promise<readonly [string, string | null][]>;
|
|
8
|
+
multiSet(keyValuePairs: Array<[string, string]>): Promise<void>;
|
|
9
|
+
multiRemove(keys: string[]): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
2
|
+
export class StorageNative {
|
|
3
|
+
async getItem(key) {
|
|
4
|
+
return await AsyncStorage.getItem(key);
|
|
5
|
+
}
|
|
6
|
+
async setItem(key, value) {
|
|
7
|
+
await AsyncStorage.setItem(key, value);
|
|
8
|
+
}
|
|
9
|
+
async removeItem(key) {
|
|
10
|
+
await AsyncStorage.removeItem(key);
|
|
11
|
+
}
|
|
12
|
+
async clear() {
|
|
13
|
+
await AsyncStorage.clear();
|
|
14
|
+
}
|
|
15
|
+
async getAllKeys() {
|
|
16
|
+
return await AsyncStorage.getAllKeys();
|
|
17
|
+
}
|
|
18
|
+
async multiGet(keys) {
|
|
19
|
+
return await AsyncStorage.multiGet(keys);
|
|
20
|
+
}
|
|
21
|
+
async multiSet(keyValuePairs) {
|
|
22
|
+
await AsyncStorage.multiSet(keyValuePairs);
|
|
23
|
+
}
|
|
24
|
+
async multiRemove(keys) {
|
|
25
|
+
await AsyncStorage.multiRemove(keys);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class StorageWeb {
|
|
2
|
+
getItem(key: string): Promise<string | null>;
|
|
3
|
+
setItem(key: string, value: string): Promise<void>;
|
|
4
|
+
removeItem(key: string): Promise<void>;
|
|
5
|
+
clear(): Promise<void>;
|
|
6
|
+
getAllKeys(): Promise<readonly string[]>;
|
|
7
|
+
multiGet(keys: string[]): Promise<readonly [string, string | null][]>;
|
|
8
|
+
multiSet(keyValuePairs: Array<[string, string]>): Promise<void>;
|
|
9
|
+
multiRemove(keys: string[]): Promise<void>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class StorageWeb {
|
|
2
|
+
async getItem(key) {
|
|
3
|
+
return localStorage.getItem(key);
|
|
4
|
+
}
|
|
5
|
+
async setItem(key, value) {
|
|
6
|
+
localStorage.setItem(key, value);
|
|
7
|
+
}
|
|
8
|
+
async removeItem(key) {
|
|
9
|
+
localStorage.removeItem(key);
|
|
10
|
+
}
|
|
11
|
+
async clear() {
|
|
12
|
+
localStorage.clear();
|
|
13
|
+
}
|
|
14
|
+
async getAllKeys() {
|
|
15
|
+
return Object.keys(localStorage);
|
|
16
|
+
}
|
|
17
|
+
async multiGet(keys) {
|
|
18
|
+
return keys.map(key => [key, localStorage.getItem(key)]);
|
|
19
|
+
}
|
|
20
|
+
async multiSet(keyValuePairs) {
|
|
21
|
+
keyValuePairs.forEach(([key, value]) => localStorage.setItem(key, value));
|
|
22
|
+
}
|
|
23
|
+
async multiRemove(keys) {
|
|
24
|
+
keys.forEach(key => localStorage.removeItem(key));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Logger
|
|
1
|
+
// Logger module main file
|
|
2
2
|
import { BaseLogger } from './BaseLogger';
|
|
3
3
|
// Export the Logger class
|
|
4
4
|
export class Logger extends BaseLogger {
|
|
@@ -7,6 +7,3 @@ export class Logger extends BaseLogger {
|
|
|
7
7
|
export const logger = new Logger();
|
|
8
8
|
// Default export (logger instance)
|
|
9
9
|
export default logger;
|
|
10
|
-
// adb shell
|
|
11
|
-
// run-as com.rnturboapp
|
|
12
|
-
// ls -lh files/localLogs
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { Logger, logger } from './logger';
|
|
2
|
+
export type { LogLevel, ILogger, ILoggerImplementation } from './logger';
|
|
3
|
+
export { LocalStorage } from './local-storage';
|
|
4
|
+
export { DatabaseManager, Database, createDatabase } from './database';
|
|
5
|
+
export type { IDatabaseAdapter, DatabaseOptions, ExecuteOptions, SaveWebDBOptions, ExportWebDBOptions, DeleteUserDataOptions, SuccessCallback, ErrorCallback, ResultCallback, SQLResultSet, SQLTransaction, SQLiteDatabase } from './database';
|
|
6
|
+
export { DeviceInfo } from './device-info';
|
|
7
|
+
export type { IDeviceInfo } from './device-info';
|
|
8
|
+
export { FileSystem } from './file-system';
|
|
9
|
+
export type { IFileEntry } from './file-system';
|
|
10
|
+
export { PushNotification } from './push-notification';
|
|
11
|
+
export type { IPushNotificationAdapter } from './push-notification';
|
package/dist/main.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// ============================================
|
|
2
|
+
// Logger Module
|
|
3
|
+
// ============================================
|
|
4
|
+
export { Logger, logger } from './logger';
|
|
5
|
+
// ============================================
|
|
6
|
+
// Local Storage Module
|
|
7
|
+
// ============================================
|
|
8
|
+
export { LocalStorage } from './local-storage';
|
|
9
|
+
// ============================================
|
|
10
|
+
// Database Module
|
|
11
|
+
// ============================================
|
|
12
|
+
export { DatabaseManager, Database, createDatabase } from './database';
|
|
13
|
+
// ============================================
|
|
14
|
+
// Device Info Module
|
|
15
|
+
// ============================================
|
|
16
|
+
export { DeviceInfo } from './device-info';
|
|
17
|
+
// ============================================
|
|
18
|
+
// File System Module
|
|
19
|
+
// ============================================
|
|
20
|
+
export { FileSystem } from './file-system';
|
|
21
|
+
// ============================================
|
|
22
|
+
// Push Notification Module
|
|
23
|
+
// ============================================
|
|
24
|
+
export { PushNotification } from './push-notification';
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { IPushNotificationAdapter } from './PushNotification.types';
|
|
2
|
+
/**
|
|
3
|
+
* Base Push Notification Class
|
|
4
|
+
* Handles push notifications using Firebase Cloud Messaging
|
|
5
|
+
*/
|
|
6
|
+
export declare class BasePushNotification implements IPushNotificationAdapter {
|
|
7
|
+
private tokenRefreshCallback;
|
|
8
|
+
private messageCallback;
|
|
9
|
+
private backgroundMessageCallback;
|
|
10
|
+
/**
|
|
11
|
+
* Request push notification permission
|
|
12
|
+
*/
|
|
13
|
+
requestPermission(options?: {
|
|
14
|
+
forceShow?: boolean;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Get FCM token
|
|
18
|
+
*/
|
|
19
|
+
getToken(): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Listen for token refresh
|
|
22
|
+
*/
|
|
23
|
+
onTokenRefresh(callback: (token: string) => void): void;
|
|
24
|
+
/**
|
|
25
|
+
* Listen for foreground messages
|
|
26
|
+
*/
|
|
27
|
+
onMessage(callback: (message: any) => void): void;
|
|
28
|
+
/**
|
|
29
|
+
* Listen for background messages
|
|
30
|
+
*/
|
|
31
|
+
onBackgroundMessage(callback: (message: any) => void): void;
|
|
32
|
+
/**
|
|
33
|
+
* Check if device supports push notifications
|
|
34
|
+
*/
|
|
35
|
+
static isSupported(): Promise<boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* Delete FCM token
|
|
38
|
+
*/
|
|
39
|
+
deleteToken(): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Get initial notification (app opened from notification)
|
|
42
|
+
*/
|
|
43
|
+
getInitialNotification(): Promise<any | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Subscribe to topic
|
|
46
|
+
*/
|
|
47
|
+
subscribeToTopic(topic: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Unsubscribe from topic
|
|
50
|
+
*/
|
|
51
|
+
unsubscribeFromTopic(topic: string): Promise<void>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// BasePushNotification.ts
|
|
2
|
+
// Base push notification implementation
|
|
3
|
+
let messaging;
|
|
4
|
+
let FirebaseMessagingTypes;
|
|
5
|
+
try {
|
|
6
|
+
const firebaseMessaging = require('@react-native-firebase/messaging');
|
|
7
|
+
messaging = firebaseMessaging.default;
|
|
8
|
+
FirebaseMessagingTypes = firebaseMessaging.FirebaseMessagingTypes;
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
console.warn('Firebase messaging not installed. Push notifications will not work.');
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Base Push Notification Class
|
|
15
|
+
* Handles push notifications using Firebase Cloud Messaging
|
|
16
|
+
*/
|
|
17
|
+
export class BasePushNotification {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.tokenRefreshCallback = null;
|
|
20
|
+
this.messageCallback = null;
|
|
21
|
+
this.backgroundMessageCallback = null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Request push notification permission
|
|
25
|
+
*/
|
|
26
|
+
async requestPermission(options = {}) {
|
|
27
|
+
if (!messaging) {
|
|
28
|
+
throw new Error('Firebase messaging is not installed');
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const authStatus = await messaging().requestPermission();
|
|
32
|
+
const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
|
|
33
|
+
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
|
|
34
|
+
if (enabled) {
|
|
35
|
+
console.log('Push notification permission granted:', authStatus);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.log('Push notification permission denied');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
console.error('Error requesting push notification permission:', error);
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get FCM token
|
|
48
|
+
*/
|
|
49
|
+
async getToken() {
|
|
50
|
+
if (!messaging) {
|
|
51
|
+
throw new Error('Firebase messaging is not installed');
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const token = await messaging().getToken();
|
|
55
|
+
console.log('FCM Token:', token);
|
|
56
|
+
return token;
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error('Error getting FCM token:', error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Listen for token refresh
|
|
65
|
+
*/
|
|
66
|
+
onTokenRefresh(callback) {
|
|
67
|
+
if (!messaging)
|
|
68
|
+
return;
|
|
69
|
+
this.tokenRefreshCallback = callback;
|
|
70
|
+
messaging().onTokenRefresh((token) => {
|
|
71
|
+
console.log('FCM Token refreshed:', token);
|
|
72
|
+
if (this.tokenRefreshCallback) {
|
|
73
|
+
this.tokenRefreshCallback(token);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Listen for foreground messages
|
|
79
|
+
*/
|
|
80
|
+
onMessage(callback) {
|
|
81
|
+
if (!messaging)
|
|
82
|
+
return;
|
|
83
|
+
this.messageCallback = callback;
|
|
84
|
+
messaging().onMessage(async (remoteMessage) => {
|
|
85
|
+
console.log('Foreground message received:', remoteMessage);
|
|
86
|
+
if (this.messageCallback) {
|
|
87
|
+
this.messageCallback(remoteMessage);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Listen for background messages
|
|
93
|
+
*/
|
|
94
|
+
onBackgroundMessage(callback) {
|
|
95
|
+
if (!messaging)
|
|
96
|
+
return;
|
|
97
|
+
this.backgroundMessageCallback = callback;
|
|
98
|
+
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
|
|
99
|
+
console.log('Background message received:', remoteMessage);
|
|
100
|
+
if (this.backgroundMessageCallback) {
|
|
101
|
+
this.backgroundMessageCallback(remoteMessage);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Check if device supports push notifications
|
|
107
|
+
*/
|
|
108
|
+
static async isSupported() {
|
|
109
|
+
if (!messaging)
|
|
110
|
+
return false;
|
|
111
|
+
try {
|
|
112
|
+
return await messaging().isDeviceRegisteredForRemoteMessages();
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Delete FCM token
|
|
120
|
+
*/
|
|
121
|
+
async deleteToken() {
|
|
122
|
+
if (!messaging) {
|
|
123
|
+
throw new Error('Firebase messaging is not installed');
|
|
124
|
+
}
|
|
125
|
+
try {
|
|
126
|
+
await messaging().deleteToken();
|
|
127
|
+
console.log('FCM token deleted');
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.error('Error deleting FCM token:', error);
|
|
131
|
+
throw error;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get initial notification (app opened from notification)
|
|
136
|
+
*/
|
|
137
|
+
async getInitialNotification() {
|
|
138
|
+
if (!messaging)
|
|
139
|
+
return null;
|
|
140
|
+
try {
|
|
141
|
+
return await messaging().getInitialNotification();
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error('Error getting initial notification:', error);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Subscribe to topic
|
|
150
|
+
*/
|
|
151
|
+
async subscribeToTopic(topic) {
|
|
152
|
+
if (!messaging) {
|
|
153
|
+
throw new Error('Firebase messaging is not installed');
|
|
154
|
+
}
|
|
155
|
+
try {
|
|
156
|
+
await messaging().subscribeToTopic(topic);
|
|
157
|
+
console.log(`Subscribed to topic: ${topic}`);
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
console.error('Error subscribing to topic:', error);
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Unsubscribe from topic
|
|
166
|
+
*/
|
|
167
|
+
async unsubscribeFromTopic(topic) {
|
|
168
|
+
if (!messaging) {
|
|
169
|
+
throw new Error('Firebase messaging is not installed');
|
|
170
|
+
}
|
|
171
|
+
try {
|
|
172
|
+
await messaging().unsubscribeFromTopic(topic);
|
|
173
|
+
console.log(`Unsubscribed from topic: ${topic}`);
|
|
174
|
+
}
|
|
175
|
+
catch (error) {
|
|
176
|
+
console.error('Error unsubscribing from topic:', error);
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BasePushNotification } from './BasePushNotification';
|
|
2
|
+
/**
|
|
3
|
+
* PushNotification class
|
|
4
|
+
* Extends BasePushNotification
|
|
5
|
+
*/
|
|
6
|
+
export declare class PushNotification extends BasePushNotification {
|
|
7
|
+
}
|
|
8
|
+
export type { IPushNotificationAdapter } from './PushNotification.types';
|
|
9
|
+
export default PushNotification;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// PushNotification.ts
|
|
2
|
+
// Main push notification export
|
|
3
|
+
import { BasePushNotification } from './BasePushNotification';
|
|
4
|
+
/**
|
|
5
|
+
* PushNotification class
|
|
6
|
+
* Extends BasePushNotification
|
|
7
|
+
*/
|
|
8
|
+
export class PushNotification extends BasePushNotification {
|
|
9
|
+
}
|
|
10
|
+
// Default export
|
|
11
|
+
export default PushNotification;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Push notification adapter interface
|
|
3
|
+
*/
|
|
4
|
+
export interface IPushNotificationAdapter {
|
|
5
|
+
requestPermission(options?: {
|
|
6
|
+
forceShow?: boolean;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
getToken(): Promise<string>;
|
|
9
|
+
onTokenRefresh(callback: (token: string) => void): void;
|
|
10
|
+
onMessage(callback: (message: any) => void): void;
|
|
11
|
+
onBackgroundMessage(callback: (message: any) => void): void;
|
|
12
|
+
}
|