react-native-qpush 0.0.0-stage → 1.0.0

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.
Files changed (98) hide show
  1. package/README.md +439 -2
  2. package/android/build.gradle +77 -0
  3. package/android/gradle.properties +5 -0
  4. package/android/proguard-rules.pro +5 -0
  5. package/android/src/main/AndroidManifest.xml +29 -0
  6. package/android/src/main/java/com/qpush/reactnative/QPushDeviceInfo.kt +96 -0
  7. package/android/src/main/java/com/qpush/reactnative/QPushEventBus.kt +153 -0
  8. package/android/src/main/java/com/qpush/reactnative/QPushFcm.kt +108 -0
  9. package/android/src/main/java/com/qpush/reactnative/QPushHttp.kt +92 -0
  10. package/android/src/main/java/com/qpush/reactnative/QPushImages.kt +103 -0
  11. package/android/src/main/java/com/qpush/reactnative/QPushLog.kt +39 -0
  12. package/android/src/main/java/com/qpush/reactnative/QPushMessagingService.kt +77 -0
  13. package/android/src/main/java/com/qpush/reactnative/QPushNotificationOpenActivity.kt +121 -0
  14. package/android/src/main/java/com/qpush/reactnative/QPushNotificationRenderer.kt +256 -0
  15. package/android/src/main/java/com/qpush/reactnative/QPushPayload.kt +114 -0
  16. package/android/src/main/java/com/qpush/reactnative/QPushPushModule.kt +368 -0
  17. package/android/src/main/java/com/qpush/reactnative/QPushPushPackage.kt +15 -0
  18. package/android/src/main/java/com/qpush/reactnative/QPushStore.kt +108 -0
  19. package/android/src/main/res/values/qpush_strings.xml +6 -0
  20. package/app.plugin.js +1 -0
  21. package/ios/Extension/QPushNotificationServiceExtension.swift +261 -0
  22. package/ios/Extension/QPushPushExtension.podspec +34 -0
  23. package/ios/QPushPush.m +62 -0
  24. package/ios/QPushPush.swift +186 -0
  25. package/ios/QPushPushBridge.h +5 -0
  26. package/ios/QPushPushCore.swift +457 -0
  27. package/ios/QPushPushHooks.m +237 -0
  28. package/ios/QPushPushSupport.swift +249 -0
  29. package/ios_nse_template/Info.plist +38 -0
  30. package/ios_nse_template/NotificationService.entitlements +10 -0
  31. package/ios_nse_template/NotificationService.swift +44 -0
  32. package/lib/NativeQPushPush.d.ts +44 -0
  33. package/lib/NativeQPushPush.js +4 -0
  34. package/lib/QPush.d.ts +117 -0
  35. package/lib/QPush.js +206 -0
  36. package/lib/core/QPushCore.d.ts +148 -0
  37. package/lib/core/QPushCore.js +1387 -0
  38. package/lib/core/constants.d.ts +37 -0
  39. package/lib/core/constants.js +40 -0
  40. package/lib/core/events.d.ts +11 -0
  41. package/lib/core/events.js +40 -0
  42. package/lib/core/http.d.ts +41 -0
  43. package/lib/core/http.js +123 -0
  44. package/lib/core/logger.d.ts +18 -0
  45. package/lib/core/logger.js +43 -0
  46. package/lib/core/operationQueue.d.ts +117 -0
  47. package/lib/core/operationQueue.js +264 -0
  48. package/lib/core/operations.d.ts +80 -0
  49. package/lib/core/operations.js +88 -0
  50. package/lib/core/payload.d.ts +20 -0
  51. package/lib/core/payload.js +187 -0
  52. package/lib/core/platform.d.ts +72 -0
  53. package/lib/core/platform.js +7 -0
  54. package/lib/core/state.d.ts +51 -0
  55. package/lib/core/state.js +74 -0
  56. package/lib/core/storage.d.ts +34 -0
  57. package/lib/core/storage.js +68 -0
  58. package/lib/core/subscription.d.ts +67 -0
  59. package/lib/core/subscription.js +98 -0
  60. package/lib/core/tags.d.ts +39 -0
  61. package/lib/core/tags.js +123 -0
  62. package/lib/core/url.d.ts +17 -0
  63. package/lib/core/url.js +64 -0
  64. package/lib/core/uuid.d.ts +2 -0
  65. package/lib/core/uuid.js +18 -0
  66. package/lib/index.d.ts +7 -0
  67. package/lib/index.js +15 -0
  68. package/lib/notification.d.ts +67 -0
  69. package/lib/notification.js +73 -0
  70. package/lib/reactNativePlatform.d.ts +3 -0
  71. package/lib/reactNativePlatform.js +164 -0
  72. package/lib/types.d.ts +157 -0
  73. package/lib/types.js +31 -0
  74. package/package.json +70 -4
  75. package/plugin/withQPush.js +330 -0
  76. package/react-native-qpush.podspec +30 -0
  77. package/react-native.config.js +18 -0
  78. package/src/NativeQPushPush.ts +57 -0
  79. package/src/QPush.ts +292 -0
  80. package/src/core/QPushCore.ts +1515 -0
  81. package/src/core/constants.ts +42 -0
  82. package/src/core/events.ts +34 -0
  83. package/src/core/http.ts +146 -0
  84. package/src/core/logger.ts +42 -0
  85. package/src/core/operationQueue.ts +324 -0
  86. package/src/core/operations.ts +166 -0
  87. package/src/core/payload.ts +175 -0
  88. package/src/core/platform.ts +84 -0
  89. package/src/core/state.ts +127 -0
  90. package/src/core/storage.ts +76 -0
  91. package/src/core/subscription.ts +133 -0
  92. package/src/core/tags.ts +116 -0
  93. package/src/core/url.ts +58 -0
  94. package/src/core/uuid.ts +13 -0
  95. package/src/index.ts +23 -0
  96. package/src/notification.ts +113 -0
  97. package/src/reactNativePlatform.ts +172 -0
  98. package/src/types.ts +174 -0
package/src/QPush.ts ADDED
@@ -0,0 +1,292 @@
1
+ import { QPushCore, type CoreEvents } from './core/QPushCore';
2
+ import { Logger } from './core/logger';
3
+ import type { PlatformAdapter } from './core/platform';
4
+ import type { NotificationClickEvent, NotificationWillDisplayEvent } from './notification';
5
+ import { createReactNativePlatform } from './reactNativePlatform';
6
+ import type {
7
+ QPushInitOptions,
8
+ LogLevel,
9
+ NotificationPermission,
10
+ PushSubscriptionChangedState,
11
+ UserChangedState,
12
+ } from './types';
13
+
14
+ /** Events of `QPush.Notifications.addEventListener`. */
15
+ export interface NotificationEventTypeMap {
16
+ click: NotificationClickEvent;
17
+ foregroundWillDisplay: NotificationWillDisplayEvent;
18
+ permissionChange: boolean;
19
+ }
20
+
21
+ export type NotificationEventName = keyof NotificationEventTypeMap;
22
+
23
+ /** Events of `QPush.User.pushSubscription.addEventListener`. */
24
+ export interface PushSubscriptionEventTypeMap {
25
+ change: PushSubscriptionChangedState;
26
+ }
27
+
28
+ /** Events of `QPush.User.addEventListener`. */
29
+ export interface UserEventTypeMap {
30
+ change: UserChangedState;
31
+ }
32
+
33
+ let core: QPushCore | null = null;
34
+ let platformFactory: () => PlatformAdapter = createReactNativePlatform;
35
+
36
+ function getCore(): QPushCore {
37
+ if (!core) core = new QPushCore(platformFactory(), new Logger());
38
+ return core;
39
+ }
40
+
41
+ /** @internal Testing hook: replaces the platform used by the singleton. */
42
+ export function __setPlatformFactoryForTests(factory: () => PlatformAdapter): QPushCore {
43
+ platformFactory = factory;
44
+ core = null;
45
+ return getCore();
46
+ }
47
+
48
+ const NOTIFICATION_EVENTS: Record<NotificationEventName, true> = {
49
+ click: true,
50
+ foregroundWillDisplay: true,
51
+ permissionChange: true,
52
+ };
53
+
54
+ const Debug = {
55
+ /** Sets the SDK log level (JS console and native logs). */
56
+ setLogLevel(level: LogLevel): void {
57
+ getCore().setLogLevel(level);
58
+ },
59
+ };
60
+
61
+ const Notifications = {
62
+ /**
63
+ * Shows the OS permission prompt (Android 13+ / iOS). With
64
+ * `fallbackToSettings`, opens the app notification settings when the
65
+ * prompt can no longer be shown (permission permanently denied).
66
+ */
67
+ requestPermission(fallbackToSettings: boolean = false): Promise<boolean> {
68
+ return getCore().requestPermission(fallbackToSettings);
69
+ },
70
+
71
+ /** `true` when the app can currently display notifications. */
72
+ getPermissionAsync(): Promise<boolean> {
73
+ return getCore().getPermission();
74
+ },
75
+
76
+ /** Detailed native permission state. */
77
+ permissionNative(): Promise<NotificationPermission> {
78
+ return getCore().permissionNative();
79
+ },
80
+
81
+ /** `true` when calling `requestPermission` would show the OS prompt. */
82
+ canRequestPermission(): Promise<boolean> {
83
+ return getCore().canRequestPermission();
84
+ },
85
+
86
+ /** iOS 12+: quiet "provisional" authorization without a prompt. Android: same as getPermissionAsync. */
87
+ registerForProvisionalAuthorization(): Promise<boolean> {
88
+ return getCore().registerForProvisionalAuthorization();
89
+ },
90
+
91
+ addEventListener<K extends NotificationEventName>(
92
+ event: K,
93
+ listener: (event: NotificationEventTypeMap[K]) => void,
94
+ ): void {
95
+ if (!NOTIFICATION_EVENTS[event]) throw new Error(`Unknown QPush notification event "${String(event)}"`);
96
+ getCore().addListener(event, listener as unknown as (payload: CoreEvents[K]) => void);
97
+ },
98
+
99
+ removeEventListener<K extends NotificationEventName>(
100
+ event: K,
101
+ listener: (event: NotificationEventTypeMap[K]) => void,
102
+ ): void {
103
+ if (!NOTIFICATION_EVENTS[event]) return;
104
+ getCore().removeListener(event, listener as unknown as (payload: CoreEvents[K]) => void);
105
+ },
106
+
107
+ /** Removes every QPush notification from the notification center and clears the badge. */
108
+ clearAll(): void {
109
+ getCore().clearAll();
110
+ },
111
+
112
+ /** Removes a single notification by its QPush notification id. */
113
+ removeNotification(notificationId: string): void {
114
+ getCore().removeNotification(notificationId);
115
+ },
116
+
117
+ /** Removes the notifications of a group (Android group / iOS thread-id). */
118
+ removeGroupedNotifications(groupKey: string): void {
119
+ getCore().removeGroupedNotifications(groupKey);
120
+ },
121
+
122
+ /**
123
+ * Enables / disables opening the notification URL on tap (default enabled).
124
+ * Persisted natively, so it also applies to cold-start taps on next launches.
125
+ * Build-time switch: Android manifest meta-data `com.qpush.suppressLaunchURLs`
126
+ * / iOS Info.plist `QPush_suppress_launch_urls`.
127
+ */
128
+ setAutoOpenUrls(enabled: boolean): void {
129
+ getCore().setAutoOpenUrls(enabled);
130
+ },
131
+ };
132
+
133
+ const pushSubscription = {
134
+ /** Subscription id of this device, `null` until the server created it. */
135
+ getIdAsync(): Promise<string | null> {
136
+ return getCore().getSubscriptionId();
137
+ },
138
+
139
+ /** FCM token (Android) / hex APNs token (iOS). */
140
+ getTokenAsync(): Promise<string | null> {
141
+ return getCore().getSubscriptionToken();
142
+ },
143
+
144
+ /** `true` when the device can receive pushes (permission + token + not opted out). */
145
+ getOptedInAsync(): Promise<boolean> {
146
+ return getCore().getOptedIn();
147
+ },
148
+
149
+ /** Re-enables pushes; prompts for permission when it was never asked. */
150
+ optIn(): void {
151
+ getCore().optIn();
152
+ },
153
+
154
+ /** Disables pushes for this device without touching the OS permission. */
155
+ optOut(): void {
156
+ getCore().optOut();
157
+ },
158
+
159
+ addEventListener<K extends keyof PushSubscriptionEventTypeMap>(
160
+ _event: K,
161
+ listener: (event: PushSubscriptionEventTypeMap[K]) => void,
162
+ ): void {
163
+ getCore().addListener('pushSubscriptionChange', listener);
164
+ },
165
+
166
+ removeEventListener<K extends keyof PushSubscriptionEventTypeMap>(
167
+ _event: K,
168
+ listener: (event: PushSubscriptionEventTypeMap[K]) => void,
169
+ ): void {
170
+ getCore().removeListener('pushSubscriptionChange', listener);
171
+ },
172
+ };
173
+
174
+ const User = {
175
+ pushSubscription,
176
+
177
+ /** Adds or updates a tag. Values are stored as strings. */
178
+ addTag(key: string, value: string | number | boolean): void {
179
+ getCore().addTags({ [key]: value });
180
+ },
181
+
182
+ addTags(tags: Record<string, string | number | boolean>): void {
183
+ getCore().addTags(tags);
184
+ },
185
+
186
+ removeTag(key: string): void {
187
+ getCore().removeTags([key]);
188
+ },
189
+
190
+ removeTags(keys: string[]): void {
191
+ getCore().removeTags(keys);
192
+ },
193
+
194
+ /** Local copy of the user's tags (refreshed from the server on app start and login). */
195
+ getTags(): Promise<Record<string, string>> {
196
+ return getCore().getTags();
197
+ },
198
+
199
+ /** Adds a custom alias (e.g. `crm_id`). Use `QPush.login` for `external_id`. */
200
+ addAlias(label: string, id: string): void {
201
+ getCore().addAliases({ [label]: id });
202
+ },
203
+
204
+ addAliases(aliases: Record<string, string>): void {
205
+ getCore().addAliases(aliases);
206
+ },
207
+
208
+ removeAlias(label: string): void {
209
+ getCore().removeAliases([label]);
210
+ },
211
+
212
+ removeAliases(labels: string[]): void {
213
+ getCore().removeAliases(labels);
214
+ },
215
+
216
+ /** ISO 639-1 language code, e.g. `pt`. Defaults to the device language. */
217
+ setLanguage(language: string): void {
218
+ getCore().setLanguage(language);
219
+ },
220
+
221
+ /** QPush user id (`qpush_id`), `null` until the server created the user. */
222
+ getQPushId(): Promise<string | null> {
223
+ return getCore().getQPushId();
224
+ },
225
+
226
+ /** Alias of `getQPushId` for code migrated from OneSignal. */
227
+ getOnesignalId(): Promise<string | null> {
228
+ return getCore().getQPushId();
229
+ },
230
+
231
+ getExternalId(): Promise<string | null> {
232
+ return getCore().getExternalId();
233
+ },
234
+
235
+ addEventListener<K extends keyof UserEventTypeMap>(
236
+ _event: K,
237
+ listener: (event: UserEventTypeMap[K]) => void,
238
+ ): void {
239
+ getCore().addListener('userChange', listener);
240
+ },
241
+
242
+ removeEventListener<K extends keyof UserEventTypeMap>(
243
+ _event: K,
244
+ listener: (event: UserEventTypeMap[K]) => void,
245
+ ): void {
246
+ getCore().removeListener('userChange', listener);
247
+ },
248
+ };
249
+
250
+ /**
251
+ * QPush SDK entry point.
252
+ *
253
+ * ```ts
254
+ * QPush.initialize('APP_ID'); // qpush cloud: https://qpush.dev/api/v1
255
+ * await QPush.Notifications.requestPermission(true);
256
+ * QPush.login('user-123');
257
+ * ```
258
+ */
259
+ export const QPush = {
260
+ /**
261
+ * Initializes the SDK. Call once, as early as possible (e.g. in index.js or
262
+ * your root component). Safe to call API methods right after it.
263
+ */
264
+ initialize(appId: string, options?: QPushInitOptions): void {
265
+ getCore().initialize(appId, options);
266
+ },
267
+
268
+ /** Identifies the current user with your user id (`external_id`). */
269
+ login(externalId: string): void {
270
+ getCore().login(externalId);
271
+ },
272
+
273
+ /** Detaches the device from the identified user (a new anonymous user is created). */
274
+ logout(): void {
275
+ getCore().logout();
276
+ },
277
+
278
+ /** When `true`, nothing is sent until `setConsentGiven(true)`. Call before `initialize`. */
279
+ setConsentRequired(required: boolean): void {
280
+ getCore().setConsentRequired(required);
281
+ },
282
+
283
+ setConsentGiven(given: boolean): void {
284
+ getCore().setConsentGiven(given);
285
+ },
286
+
287
+ Debug,
288
+ Notifications,
289
+ User,
290
+ };
291
+
292
+ export type QPushType = typeof QPush;