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/lib/QPush.js ADDED
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QPush = void 0;
4
+ exports.__setPlatformFactoryForTests = __setPlatformFactoryForTests;
5
+ const QPushCore_1 = require("./core/QPushCore");
6
+ const logger_1 = require("./core/logger");
7
+ const reactNativePlatform_1 = require("./reactNativePlatform");
8
+ let core = null;
9
+ let platformFactory = reactNativePlatform_1.createReactNativePlatform;
10
+ function getCore() {
11
+ if (!core)
12
+ core = new QPushCore_1.QPushCore(platformFactory(), new logger_1.Logger());
13
+ return core;
14
+ }
15
+ /** @internal Testing hook: replaces the platform used by the singleton. */
16
+ function __setPlatformFactoryForTests(factory) {
17
+ platformFactory = factory;
18
+ core = null;
19
+ return getCore();
20
+ }
21
+ const NOTIFICATION_EVENTS = {
22
+ click: true,
23
+ foregroundWillDisplay: true,
24
+ permissionChange: true,
25
+ };
26
+ const Debug = {
27
+ /** Sets the SDK log level (JS console and native logs). */
28
+ setLogLevel(level) {
29
+ getCore().setLogLevel(level);
30
+ },
31
+ };
32
+ const Notifications = {
33
+ /**
34
+ * Shows the OS permission prompt (Android 13+ / iOS). With
35
+ * `fallbackToSettings`, opens the app notification settings when the
36
+ * prompt can no longer be shown (permission permanently denied).
37
+ */
38
+ requestPermission(fallbackToSettings = false) {
39
+ return getCore().requestPermission(fallbackToSettings);
40
+ },
41
+ /** `true` when the app can currently display notifications. */
42
+ getPermissionAsync() {
43
+ return getCore().getPermission();
44
+ },
45
+ /** Detailed native permission state. */
46
+ permissionNative() {
47
+ return getCore().permissionNative();
48
+ },
49
+ /** `true` when calling `requestPermission` would show the OS prompt. */
50
+ canRequestPermission() {
51
+ return getCore().canRequestPermission();
52
+ },
53
+ /** iOS 12+: quiet "provisional" authorization without a prompt. Android: same as getPermissionAsync. */
54
+ registerForProvisionalAuthorization() {
55
+ return getCore().registerForProvisionalAuthorization();
56
+ },
57
+ addEventListener(event, listener) {
58
+ if (!NOTIFICATION_EVENTS[event])
59
+ throw new Error(`Unknown QPush notification event "${String(event)}"`);
60
+ getCore().addListener(event, listener);
61
+ },
62
+ removeEventListener(event, listener) {
63
+ if (!NOTIFICATION_EVENTS[event])
64
+ return;
65
+ getCore().removeListener(event, listener);
66
+ },
67
+ /** Removes every QPush notification from the notification center and clears the badge. */
68
+ clearAll() {
69
+ getCore().clearAll();
70
+ },
71
+ /** Removes a single notification by its QPush notification id. */
72
+ removeNotification(notificationId) {
73
+ getCore().removeNotification(notificationId);
74
+ },
75
+ /** Removes the notifications of a group (Android group / iOS thread-id). */
76
+ removeGroupedNotifications(groupKey) {
77
+ getCore().removeGroupedNotifications(groupKey);
78
+ },
79
+ /**
80
+ * Enables / disables opening the notification URL on tap (default enabled).
81
+ * Persisted natively, so it also applies to cold-start taps on next launches.
82
+ * Build-time switch: Android manifest meta-data `com.qpush.suppressLaunchURLs`
83
+ * / iOS Info.plist `QPush_suppress_launch_urls`.
84
+ */
85
+ setAutoOpenUrls(enabled) {
86
+ getCore().setAutoOpenUrls(enabled);
87
+ },
88
+ };
89
+ const pushSubscription = {
90
+ /** Subscription id of this device, `null` until the server created it. */
91
+ getIdAsync() {
92
+ return getCore().getSubscriptionId();
93
+ },
94
+ /** FCM token (Android) / hex APNs token (iOS). */
95
+ getTokenAsync() {
96
+ return getCore().getSubscriptionToken();
97
+ },
98
+ /** `true` when the device can receive pushes (permission + token + not opted out). */
99
+ getOptedInAsync() {
100
+ return getCore().getOptedIn();
101
+ },
102
+ /** Re-enables pushes; prompts for permission when it was never asked. */
103
+ optIn() {
104
+ getCore().optIn();
105
+ },
106
+ /** Disables pushes for this device without touching the OS permission. */
107
+ optOut() {
108
+ getCore().optOut();
109
+ },
110
+ addEventListener(_event, listener) {
111
+ getCore().addListener('pushSubscriptionChange', listener);
112
+ },
113
+ removeEventListener(_event, listener) {
114
+ getCore().removeListener('pushSubscriptionChange', listener);
115
+ },
116
+ };
117
+ const User = {
118
+ pushSubscription,
119
+ /** Adds or updates a tag. Values are stored as strings. */
120
+ addTag(key, value) {
121
+ getCore().addTags({ [key]: value });
122
+ },
123
+ addTags(tags) {
124
+ getCore().addTags(tags);
125
+ },
126
+ removeTag(key) {
127
+ getCore().removeTags([key]);
128
+ },
129
+ removeTags(keys) {
130
+ getCore().removeTags(keys);
131
+ },
132
+ /** Local copy of the user's tags (refreshed from the server on app start and login). */
133
+ getTags() {
134
+ return getCore().getTags();
135
+ },
136
+ /** Adds a custom alias (e.g. `crm_id`). Use `QPush.login` for `external_id`. */
137
+ addAlias(label, id) {
138
+ getCore().addAliases({ [label]: id });
139
+ },
140
+ addAliases(aliases) {
141
+ getCore().addAliases(aliases);
142
+ },
143
+ removeAlias(label) {
144
+ getCore().removeAliases([label]);
145
+ },
146
+ removeAliases(labels) {
147
+ getCore().removeAliases(labels);
148
+ },
149
+ /** ISO 639-1 language code, e.g. `pt`. Defaults to the device language. */
150
+ setLanguage(language) {
151
+ getCore().setLanguage(language);
152
+ },
153
+ /** QPush user id (`qpush_id`), `null` until the server created the user. */
154
+ getQPushId() {
155
+ return getCore().getQPushId();
156
+ },
157
+ /** Alias of `getQPushId` for code migrated from OneSignal. */
158
+ getOnesignalId() {
159
+ return getCore().getQPushId();
160
+ },
161
+ getExternalId() {
162
+ return getCore().getExternalId();
163
+ },
164
+ addEventListener(_event, listener) {
165
+ getCore().addListener('userChange', listener);
166
+ },
167
+ removeEventListener(_event, listener) {
168
+ getCore().removeListener('userChange', listener);
169
+ },
170
+ };
171
+ /**
172
+ * QPush SDK entry point.
173
+ *
174
+ * ```ts
175
+ * QPush.initialize('APP_ID'); // qpush cloud: https://qpush.dev/api/v1
176
+ * await QPush.Notifications.requestPermission(true);
177
+ * QPush.login('user-123');
178
+ * ```
179
+ */
180
+ exports.QPush = {
181
+ /**
182
+ * Initializes the SDK. Call once, as early as possible (e.g. in index.js or
183
+ * your root component). Safe to call API methods right after it.
184
+ */
185
+ initialize(appId, options) {
186
+ getCore().initialize(appId, options);
187
+ },
188
+ /** Identifies the current user with your user id (`external_id`). */
189
+ login(externalId) {
190
+ getCore().login(externalId);
191
+ },
192
+ /** Detaches the device from the identified user (a new anonymous user is created). */
193
+ logout() {
194
+ getCore().logout();
195
+ },
196
+ /** When `true`, nothing is sent until `setConsentGiven(true)`. Call before `initialize`. */
197
+ setConsentRequired(required) {
198
+ getCore().setConsentRequired(required);
199
+ },
200
+ setConsentGiven(given) {
201
+ getCore().setConsentGiven(given);
202
+ },
203
+ Debug,
204
+ Notifications,
205
+ User,
206
+ };
@@ -0,0 +1,148 @@
1
+ import { NotificationWillDisplayEvent, type NotificationClickEvent } from '../notification';
2
+ import { LogLevel, NotificationPermission, type QPushInitOptions, type PermissionStatus, type PushSubscriptionChangedState, type UserChangedState } from '../types';
3
+ import { EventHub } from './events';
4
+ import { Logger } from './logger';
5
+ import { OperationQueue } from './operationQueue';
6
+ import type { PlatformAdapter } from './platform';
7
+ export interface CoreEvents {
8
+ click: NotificationClickEvent;
9
+ foregroundWillDisplay: NotificationWillDisplayEvent;
10
+ permissionChange: boolean;
11
+ pushSubscriptionChange: PushSubscriptionChangedState;
12
+ userChange: UserChangedState;
13
+ }
14
+ /** Language code from a locale (`pt-BR` -> `pt`, `iw` -> `he`). */
15
+ export declare function languageFromLocale(locale: string | undefined): string | undefined;
16
+ /**
17
+ * The single implementation of the QPush user / subscription / HTTP state
18
+ * machine. Platform specifics are injected through {@link PlatformAdapter}.
19
+ */
20
+ export declare class QPushCore {
21
+ private readonly platform;
22
+ readonly logger: Logger;
23
+ readonly events: EventHub<CoreEvents>;
24
+ readonly queue: OperationQueue;
25
+ private readonly http;
26
+ private readonly stateWriter;
27
+ private state;
28
+ private loadPromise;
29
+ private commandChain;
30
+ private device;
31
+ private initializeCalled;
32
+ private started;
33
+ private registrationSettled;
34
+ private registering;
35
+ private halted;
36
+ private foregroundAt;
37
+ private backgroundAt;
38
+ private pendingColdSession;
39
+ private appStateSubscription;
40
+ private nativeSubscriptions;
41
+ private pendingClicks;
42
+ private clickFlushScheduled;
43
+ constructor(platform: PlatformAdapter, logger?: Logger);
44
+ private uuid;
45
+ private ensureLoaded;
46
+ private load;
47
+ /** Enqueues `createUser` for the current user unless it exists or is being created. */
48
+ private ensureUserCreation;
49
+ /** Runs `fn` after state is loaded, serialized with every other command. */
50
+ private command;
51
+ /** Reads state after every previously issued command ran. */
52
+ private read;
53
+ private save;
54
+ private canProcess;
55
+ private hasConsent;
56
+ private sleep;
57
+ private get appId();
58
+ /** Waits for pending commands, queue processing and storage writes (tests). */
59
+ flush(): Promise<void>;
60
+ setLogLevel(level: LogLevel): void;
61
+ initialize(appId: string, options?: QPushInitOptions): void;
62
+ private resetForNewApp;
63
+ private writeNative;
64
+ setConsentRequired(required: boolean): void;
65
+ setConsentGiven(given: boolean): void;
66
+ private maybeStart;
67
+ private start;
68
+ /** Retries push registration when the app comes back without a token (offline start, FCM params added later). */
69
+ private retryRegistrationIfNeeded;
70
+ private fetchParams;
71
+ private registerPush;
72
+ private applyRegistration;
73
+ private applyToken;
74
+ /** Reads the OS permission and updates the subscription when it changed. */
75
+ refreshPermission(emit: boolean): Promise<PermissionStatus>;
76
+ requestPermission(fallbackToSettings: boolean): Promise<boolean>;
77
+ registerForProvisionalAuthorization(): Promise<boolean>;
78
+ getPermission(): Promise<boolean>;
79
+ permissionNative(): Promise<NotificationPermission>;
80
+ canRequestPermission(): Promise<boolean>;
81
+ private subscriptionInputs;
82
+ private currentFields;
83
+ private pushState;
84
+ /** Applies a subscription change, emits `change` and schedules a sync. */
85
+ private mutateSubscription;
86
+ private scheduleSyncIfDirty;
87
+ private setSubscriptionId;
88
+ optIn(): void;
89
+ optOut(): void;
90
+ getSubscriptionId(): Promise<string | null>;
91
+ getSubscriptionToken(): Promise<string | null>;
92
+ getOptedIn(): Promise<boolean>;
93
+ private userState;
94
+ private mutateUser;
95
+ login(externalId: string): void;
96
+ logout(): void;
97
+ addTags(tags: Record<string, unknown>): void;
98
+ removeTags(keys: string[]): void;
99
+ private applyLocalTagChanges;
100
+ getTags(): Promise<Record<string, string>>;
101
+ addAliases(aliases: Record<string, string>): void;
102
+ removeAliases(labels: string[]): void;
103
+ setLanguage(language: string): void;
104
+ getQPushId(): Promise<string | null>;
105
+ getExternalId(): Promise<string | null>;
106
+ private attachAppState;
107
+ private startSession;
108
+ private onAppStateChange;
109
+ private flushInBackground;
110
+ private clearBadge;
111
+ /** Subscribes to native events and tells native that JS is ready. Idempotent. */
112
+ attachNativeEvents(): void;
113
+ private handleNativeClick;
114
+ private emitClick;
115
+ private handleNativeForeground;
116
+ addListener<K extends keyof CoreEvents>(event: K, listener: (payload: CoreEvents[K]) => void): void;
117
+ removeListener<K extends keyof CoreEvents>(event: K, listener: (payload: CoreEvents[K]) => void): void;
118
+ clearAll(): void;
119
+ removeNotification(notificationId: string): void;
120
+ removeGroupedNotifications(groupKey: string): void;
121
+ setAutoOpenUrls(enabled: boolean): void;
122
+ private execute;
123
+ private userPath;
124
+ private userProperties;
125
+ /** Tag changes still waiting in the queue for a user (to re-apply over server data). */
126
+ private pendingTagChanges;
127
+ private pendingAliasChanges;
128
+ private mapUser;
129
+ /** Keeps only ids still referenced by the current user or pending operations. */
130
+ private pruneIdMap;
131
+ /** Picks this device's subscription in a user response. */
132
+ private findOwnSubscription;
133
+ /** Applies a `POST /users` response (createUser, login fallback). */
134
+ private applyCreatedUser;
135
+ private postUser;
136
+ private execCreateUser;
137
+ private execLogin;
138
+ private handleMissingUser;
139
+ private resolveQPushId;
140
+ private execUpdateUser;
141
+ private execSetAlias;
142
+ private execRemoveAlias;
143
+ private execFetchUser;
144
+ private execSyncSubscription;
145
+ private execCreateSubscription;
146
+ /** @internal Used by the facade to know whether `initialize` was called. */
147
+ get isInitialized(): boolean;
148
+ }