@trycourier/courier-react-native 0.1.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 (47) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +382 -0
  3. package/android/.gradle/7.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  4. package/android/.gradle/7.1/dependencies-accessors/gc.properties +0 -0
  5. package/android/.gradle/7.1/executionHistory/executionHistory.lock +0 -0
  6. package/android/.gradle/7.1/fileChanges/last-build.bin +0 -0
  7. package/android/.gradle/7.1/fileHashes/fileHashes.bin +0 -0
  8. package/android/.gradle/7.1/fileHashes/fileHashes.lock +0 -0
  9. package/android/.gradle/7.1/gc.properties +0 -0
  10. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  11. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  12. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  13. package/android/.gradle/checksums/checksums.lock +0 -0
  14. package/android/.gradle/checksums/md5-checksums.bin +0 -0
  15. package/android/.gradle/checksums/sha1-checksums.bin +0 -0
  16. package/android/.gradle/vcs-1/gc.properties +0 -0
  17. package/android/.idea/compiler.xml +6 -0
  18. package/android/.idea/gradle.xml +17 -0
  19. package/android/.idea/jarRepositories.xml +35 -0
  20. package/android/.idea/misc.xml +10 -0
  21. package/android/.idea/vcs.xml +6 -0
  22. package/android/build.gradle +149 -0
  23. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  24. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  25. package/android/gradle.properties +5 -0
  26. package/android/gradlew +185 -0
  27. package/android/gradlew.bat +89 -0
  28. package/android/local.properties +8 -0
  29. package/android/src/main/AndroidManifest.xml +4 -0
  30. package/android/src/main/java/com/courierreactnative/CourierReactNativeActivity.kt +62 -0
  31. package/android/src/main/java/com/courierreactnative/CourierReactNativeModule.kt +199 -0
  32. package/android/src/main/java/com/courierreactnative/CourierReactNativePackage.kt +19 -0
  33. package/android/src/main/java/com/courierreactnative/NotificationPermissionStatus.kt +6 -0
  34. package/courier-react-native.podspec +37 -0
  35. package/ios/CourierReactNative-Bridging-Header.h +3 -0
  36. package/ios/CourierReactNative.m +53 -0
  37. package/ios/CourierReactNative.swift +286 -0
  38. package/ios/CourierReactNative.xcodeproj/project.pbxproj +283 -0
  39. package/ios/CourierReactNativeDelegate.h +20 -0
  40. package/ios/CourierReactNativeDelegate.m +125 -0
  41. package/lib/commonjs/index.js +305 -0
  42. package/lib/commonjs/index.js.map +1 -0
  43. package/lib/module/index.js +295 -0
  44. package/lib/module/index.js.map +1 -0
  45. package/lib/typescript/index.d.ts +140 -0
  46. package/package.json +156 -0
  47. package/src/index.ts +337 -0
package/src/index.ts ADDED
@@ -0,0 +1,337 @@
1
+ /* eslint-disable */
2
+ import {
3
+ NativeModules,
4
+ Platform,
5
+ DeviceEventEmitter,
6
+ NativeEventEmitter,
7
+ EmitterSubscription,
8
+ } from 'react-native';
9
+
10
+ const LINKING_ERROR =
11
+ `The package '@trycourier/courier-react-native' doesn't seem to be linked. Make sure: \n\n` +
12
+ Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
13
+ '- You rebuilt the app after installing the package\n' +
14
+ '- You are not using Expo managed workflow\n';
15
+
16
+ const CourierReactNativeModules = NativeModules.CourierReactNative
17
+ ? NativeModules.CourierReactNative
18
+ : new Proxy(
19
+ {},
20
+ {
21
+ get() {
22
+ throw new Error(LINKING_ERROR);
23
+ },
24
+ }
25
+ );
26
+
27
+ const CourierEventEmitter = new NativeEventEmitter(
28
+ NativeModules.CourierReactNative
29
+ );
30
+
31
+ export enum CourierProvider {
32
+ FCM = 'firebase-fcm',
33
+ APNS = 'apn',
34
+ }
35
+
36
+ class Courier {
37
+ readonly PUSH_NOTIFICATION_CLICKED = 'pushNotificationClicked';
38
+ readonly PUSH_NOTIFICATION_DELIVERED = 'pushNotificationDelivered';
39
+
40
+ public constructor() {
41
+ // Sets the initial SDK values
42
+ // Defaults to React Native level debugging
43
+ // and will show all foreground notification styles in iOS
44
+ this.setDefaults();
45
+ }
46
+
47
+ private async setDefaults() {
48
+ try {
49
+ await Promise.all([
50
+ this.setIsDebugging(__DEV__),
51
+ this.iOSForegroundPresentationOptions({
52
+ options: ['sound', 'badge', 'list', 'banner'],
53
+ }),
54
+ ]);
55
+ } catch (error) {
56
+ console.log(error);
57
+ }
58
+ }
59
+
60
+ private _isDebugging = false;
61
+ private debugListener: EmitterSubscription | undefined;
62
+
63
+ /**
64
+ * Tells native Courier SDKs to show or hide logs.
65
+ * Defaults to the React __DEV__ mode
66
+ * @example Courier.setIsDebugging(true)
67
+ */
68
+ public async setIsDebugging(isDebugging: boolean): Promise<boolean> {
69
+ this._isDebugging = await CourierReactNativeModules.setDebugMode(
70
+ isDebugging
71
+ );
72
+
73
+ // Remove the existing listener if needed
74
+ this.debugListener?.remove();
75
+
76
+ // Set a new listener
77
+ if (this._isDebugging) {
78
+ this.debugListener = CourierEventEmitter.addListener(
79
+ 'courierDebugEvent',
80
+ (event) => {
81
+ console.log('\x1b[36m%s\x1b[0m', 'COURIER', event);
82
+ }
83
+ );
84
+ }
85
+
86
+ return this._isDebugging;
87
+ }
88
+
89
+ get isDebugging(): boolean {
90
+ return this._isDebugging;
91
+ }
92
+
93
+ /**
94
+ * Returns the current user id stored in local native storage
95
+ * @example const userId = await Courier.userId
96
+ */
97
+ get userId(): Promise<string | undefined> {
98
+ return CourierReactNativeModules.getUserId();
99
+ }
100
+
101
+ /**
102
+ * Signs user in and persists signin in between sessions
103
+ * using native level storage apis
104
+ *
105
+ * @example
106
+ * ```
107
+ *await Courier.signIn({
108
+ accessToken: YOUR_COURIER_GENERATED_JWT,
109
+ userId: YOUR_USER_ID,
110
+ })
111
+ * ```
112
+ * Your access token should be generated using this endpoint
113
+ * that is requested from your backend
114
+ * https://www.courier.com/docs/reference/auth/issue-token/
115
+ */
116
+ public signIn({
117
+ accessToken,
118
+ userId,
119
+ }: {
120
+ accessToken: string;
121
+ userId: string;
122
+ }): Promise<void> {
123
+ return CourierReactNativeModules.signIn(userId, accessToken);
124
+ }
125
+
126
+ /**
127
+ * Logs user out of native level user storage.
128
+ * This will clear the userId, accessToken, and apns / fcm tokens and
129
+ * delete the matching devices apns / fcm tokens for the user in Courier token management
130
+ * @example await Courier.signOut()
131
+ */
132
+ public signOut(): Promise<void> {
133
+ return CourierReactNativeModules.signOut();
134
+ }
135
+
136
+ /**
137
+ * Sets the current Apple Push Notification Service (APNS) token
138
+ * using Courier token management apis
139
+ * @example const apnsToken = await Courier.apnsToken
140
+ */
141
+ get apnsToken(): Promise<string | undefined> {
142
+ if (Platform.OS !== 'ios') return Promise.resolve(undefined);
143
+ return CourierReactNativeModules.getApnsToken();
144
+ }
145
+
146
+ /**
147
+ * Sets the current Firebase Cloud Messaging (FCM) token
148
+ * using Courier token management apis
149
+ * @example const fcmToken = await Courier.fcmToken
150
+ */
151
+ get fcmToken(): Promise<string | undefined> {
152
+ return CourierReactNativeModules.getFcmToken();
153
+ }
154
+
155
+ /**
156
+ * Sets the current Firebase Cloud Messaging (FCM) token
157
+ * using Courier token management apis
158
+ * @example await setFcmToken('asdf...asdf')
159
+ */
160
+ public setFcmToken(token: string): Promise<void> {
161
+ return CourierReactNativeModules.setFcmToken(token);
162
+ }
163
+
164
+ /**
165
+ * Hits the Courier /send endpoint and sends a test push notification
166
+ * @example
167
+ * ```
168
+ *const messageId = await sendPush({
169
+ authKey: YOUR_AUTH_KEY_THAT_SHOULD_NOT_STAY_IN_YOUR_PRODUCTION_APP,
170
+ userId: USER_ID,
171
+ title: 'This is a title',
172
+ body: 'This is a body',
173
+ providers: [CourierProvider.APNS, CourierProvider.FCM],
174
+ isProduction: false, // true is production apns, false is sandbox apns
175
+ });
176
+ * ```
177
+ * @returns promise
178
+ */
179
+ public sendPush({
180
+ authKey,
181
+ userId,
182
+ title,
183
+ body,
184
+ providers,
185
+ isProduction,
186
+ }: {
187
+ authKey: string;
188
+ userId: string;
189
+ title?: string;
190
+ body?: string;
191
+ providers: CourierProvider[];
192
+ isProduction: boolean;
193
+ }): Promise<string> {
194
+ return CourierReactNativeModules.sendPush(
195
+ authKey,
196
+ userId,
197
+ title,
198
+ body,
199
+ providers,
200
+ isProduction
201
+ );
202
+ }
203
+
204
+ /**
205
+ * Gets notification permission status at a system level.
206
+ * @example const permissionStatus = await Courier.getNotificationPermissionStatus()
207
+ */
208
+ get notificationPermissionStatus(): Promise<string> {
209
+ return CourierReactNativeModules.getNotificationPermissionStatus();
210
+ }
211
+
212
+ /**
213
+ * Requests notification permission status at a system level.
214
+ * Returns the string associated with the permission status.
215
+ * Will return the current status and will not present a popup
216
+ * if the user has already been asked for permission.
217
+ * @example const permissionStatus = await Courier.requestNotificationPermission()
218
+ */
219
+ public requestNotificationPermission(): Promise<string> {
220
+ return CourierReactNativeModules.requestNotificationPermission();
221
+ }
222
+
223
+ /**
224
+ * Sets the push notification presentation style when the app is in the foreground
225
+ * This does not affect how the notification is shown when the app is killed or in the background states
226
+ *
227
+ * Defaults to sound, badge, list and/or banner.
228
+ *
229
+ * @example iOSForegroundPresentationOptions({options: ['sound']});
230
+ */
231
+ public iOSForegroundPresentationOptions({
232
+ options,
233
+ }: {
234
+ options: ('sound' | 'badge' | 'list' | 'banner')[];
235
+ }): Promise<void> {
236
+ // Only works on iOS
237
+ if (Platform.OS !== 'ios') return Promise.resolve();
238
+
239
+ const normalizedParams = Array.from(new Set(options));
240
+ return CourierReactNativeModules.iOSForegroundPresentationOptions({
241
+ options: normalizedParams,
242
+ });
243
+ }
244
+
245
+ /**
246
+ * @example
247
+ *```
248
+ const unsubPushListeners = () => {
249
+ return Courier.registerPushNotificationListeners<YOUR_NOTIFICATION_TYPE>({
250
+ onPushNotificationClicked: (push) => {
251
+ ...
252
+ },
253
+ onPushNotificationDelivered: (push) => {
254
+ ...
255
+ },
256
+ })
257
+ }
258
+
259
+ // To unsubscribe the listeners
260
+ unsubPushListeners()
261
+ *```
262
+ * @returns function that can be used to unsubscribe from registered listeners
263
+ */
264
+ public registerPushNotificationListeners({
265
+ onPushNotificationClicked,
266
+ onPushNotificationDelivered,
267
+ }: {
268
+ onPushNotificationClicked: (push: any) => void;
269
+ onPushNotificationDelivered: (push: any) => void;
270
+ }) {
271
+ let notificationClickedListener: EmitterSubscription;
272
+ let notificationDeliveredListener: EmitterSubscription;
273
+
274
+ // Android
275
+ if (Platform.OS === 'android') {
276
+ notificationClickedListener = DeviceEventEmitter.addListener(
277
+ this.PUSH_NOTIFICATION_CLICKED,
278
+ (event: any) => {
279
+ try {
280
+ onPushNotificationClicked(JSON.parse(event));
281
+ } catch (error) {
282
+ console.log(error);
283
+ }
284
+ }
285
+ );
286
+
287
+ notificationDeliveredListener = DeviceEventEmitter.addListener(
288
+ this.PUSH_NOTIFICATION_DELIVERED,
289
+ (event: any) => {
290
+ try {
291
+ onPushNotificationDelivered(JSON.parse(event));
292
+ } catch (error) {
293
+ console.log(error);
294
+ }
295
+ }
296
+ );
297
+ }
298
+
299
+ // iOS
300
+ if (Platform.OS === 'ios') {
301
+ notificationClickedListener = CourierEventEmitter.addListener(
302
+ this.PUSH_NOTIFICATION_CLICKED,
303
+ (event: any) => {
304
+ try {
305
+ onPushNotificationClicked(JSON.parse(event));
306
+ } catch (error) {
307
+ console.log(error);
308
+ }
309
+ }
310
+ );
311
+
312
+ notificationDeliveredListener = CourierEventEmitter.addListener(
313
+ this.PUSH_NOTIFICATION_DELIVERED,
314
+ (event: any) => {
315
+ try {
316
+ onPushNotificationDelivered(JSON.parse(event));
317
+ } catch (error) {
318
+ console.log(error);
319
+ }
320
+ }
321
+ );
322
+ }
323
+
324
+ // When listener is registered
325
+ // Attempt to fetch the last message that was clicked
326
+ // This is needed for when the app is killed and the
327
+ // user launched the app by clicking on a notifications
328
+ CourierReactNativeModules.registerPushNotificationClickedOnKilledState();
329
+
330
+ return () => {
331
+ notificationClickedListener.remove();
332
+ notificationDeliveredListener.remove();
333
+ };
334
+ }
335
+ }
336
+
337
+ export default new Courier();