@trycourier/courier-react-native 2.0.0-beta1 → 2.0.0-beta3
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/ios/CourierReactNative-Bridging-Header.h +1 -0
- package/ios/CourierReactNative.xcodeproj/project.pbxproj +5 -3
- package/ios/CourierReactNative.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
- package/ios/CourierReactNative.xcodeproj/project.xcworkspace/xcuserdata/mike.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- package/ios/CourierReactNative.xcodeproj/xcuserdata/mike.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
- package/ios/CourierReactNativeDelegate.h +18 -0
- package/ios/CourierReactNativeDelegate.m +125 -0
- package/ios/CourierReactNativeModule.m +54 -2
- package/ios/CourierReactNativeModule.swift +104 -70
- package/ios/CourierReactNativeViewManager.m +1 -3
- package/ios/CourierReactNativeViewManager.swift +14 -18
- package/lib/commonjs/hooks/useCourier.js +224 -0
- package/lib/commonjs/hooks/useCourier.js.map +1 -0
- package/lib/commonjs/index.js +215 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/models/CourierAuthenticationListener.js +21 -0
- package/lib/commonjs/models/CourierAuthenticationListener.js.map +1 -0
- package/lib/commonjs/models/CourierPushListener.js +15 -0
- package/lib/commonjs/models/CourierPushListener.js.map +1 -0
- package/lib/commonjs/views/CourierInboxView.js +4 -2
- package/lib/commonjs/views/CourierInboxView.js.map +1 -1
- package/lib/module/hooks/useCourier.js +213 -0
- package/lib/module/hooks/useCourier.js.map +1 -0
- package/lib/module/index.js +188 -14
- package/lib/module/index.js.map +1 -1
- package/lib/module/models/CourierAuthenticationListener.js +13 -0
- package/lib/module/models/CourierAuthenticationListener.js.map +1 -0
- package/lib/module/models/CourierPushListener.js +8 -0
- package/lib/module/models/CourierPushListener.js.map +1 -0
- package/lib/module/views/CourierInboxView.js +4 -2
- package/lib/module/views/CourierInboxView.js.map +1 -1
- package/lib/typescript/hooks/useCourier.d.ts +52 -0
- package/lib/typescript/hooks/useCourier.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +96 -2
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/models/CourierAuthenticationListener.d.ts +7 -0
- package/lib/typescript/models/CourierAuthenticationListener.d.ts.map +1 -0
- package/lib/typescript/models/CourierPushListener.d.ts +7 -0
- package/lib/typescript/models/CourierPushListener.d.ts.map +1 -0
- package/lib/typescript/views/CourierInboxView.d.ts +4 -2
- package/lib/typescript/views/CourierInboxView.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useCourier.tsx +291 -0
- package/src/index.tsx +208 -15
- package/src/models/CourierAuthenticationListener.tsx +19 -0
- package/src/models/CourierPushListener.tsx +13 -0
- package/src/views/CourierInboxView.tsx +5 -4
package/src/index.tsx
CHANGED
|
@@ -3,11 +3,21 @@ import {
|
|
|
3
3
|
NativeEventEmitter,
|
|
4
4
|
EmitterSubscription,
|
|
5
5
|
Platform,
|
|
6
|
+
DeviceEventEmitter,
|
|
6
7
|
} from 'react-native';
|
|
7
8
|
|
|
9
|
+
// Imports
|
|
8
10
|
import { CourierInboxListener } from './models/CourierInboxListener';
|
|
11
|
+
import { CourierPushListener } from './models/CourierPushListener';
|
|
12
|
+
import { CourierAuthenticationListener } from './models/CourierAuthenticationListener';
|
|
9
13
|
import { InboxMessage } from './models/InboxMessage';
|
|
14
|
+
|
|
15
|
+
// Exports
|
|
10
16
|
export { CourierInboxView } from './views/CourierInboxView';
|
|
17
|
+
export { CourierProvider, useCourier } from './hooks/useCourier';
|
|
18
|
+
export { CourierInboxListener } from './models/CourierInboxListener';
|
|
19
|
+
export { CourierPushListener } from './models/CourierPushListener';
|
|
20
|
+
export { CourierAuthenticationListener } from './models/CourierAuthenticationListener';
|
|
11
21
|
|
|
12
22
|
const LINKING_ERROR =
|
|
13
23
|
`The package '@trycourier/courier-react-native' doesn't seem to be linked. Make sure: \n\n` +
|
|
@@ -32,6 +42,9 @@ const CourierEventEmitter = new NativeEventEmitter(
|
|
|
32
42
|
|
|
33
43
|
class Courier {
|
|
34
44
|
|
|
45
|
+
readonly PUSH_NOTIFICATION_CLICKED = 'pushNotificationClicked';
|
|
46
|
+
readonly PUSH_NOTIFICATION_DELIVERED = 'pushNotificationDelivered';
|
|
47
|
+
|
|
35
48
|
private static _sharedInstance: Courier;
|
|
36
49
|
private _isDebugging = false;
|
|
37
50
|
private debugListener: EmitterSubscription | undefined;
|
|
@@ -56,16 +69,8 @@ class Courier {
|
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
private async setDefaults() {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.setIsDebugging(__DEV__),
|
|
62
|
-
// this.iOSForegroundPresentationOptions({
|
|
63
|
-
// options: ['sound', 'badge', 'list', 'banner'],
|
|
64
|
-
// }),
|
|
65
|
-
]);
|
|
66
|
-
} catch (error) {
|
|
67
|
-
console.log(error);
|
|
68
|
-
}
|
|
72
|
+
this.setIsDebugging(__DEV__);
|
|
73
|
+
this.iOSForegroundPresentationOptions({ options: ['sound', 'badge', 'list', 'banner'] });
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
/**
|
|
@@ -73,7 +78,7 @@ class Courier {
|
|
|
73
78
|
* Defaults to the React __DEV__ mode
|
|
74
79
|
* @example Courier.setIsDebugging(true)
|
|
75
80
|
*/
|
|
76
|
-
public
|
|
81
|
+
public setIsDebugging(isDebugging: boolean): boolean {
|
|
77
82
|
|
|
78
83
|
// Remove the existing listener if needed
|
|
79
84
|
this.debugListener?.remove();
|
|
@@ -86,7 +91,9 @@ class Courier {
|
|
|
86
91
|
});
|
|
87
92
|
}
|
|
88
93
|
|
|
89
|
-
|
|
94
|
+
CourierReactNativeModules.setDebugMode(isDebugging);
|
|
95
|
+
|
|
96
|
+
this._isDebugging = isDebugging
|
|
90
97
|
|
|
91
98
|
return this._isDebugging;
|
|
92
99
|
|
|
@@ -96,12 +103,143 @@ class Courier {
|
|
|
96
103
|
return this._isDebugging;
|
|
97
104
|
}
|
|
98
105
|
|
|
106
|
+
/**
|
|
107
|
+
* TODO
|
|
108
|
+
* @param props
|
|
109
|
+
* @returns
|
|
110
|
+
*/
|
|
111
|
+
public iOSForegroundPresentationOptions(props: { options: ('sound' | 'badge' | 'list' | 'banner')[] }): string {
|
|
112
|
+
|
|
113
|
+
// Only works on iOS
|
|
114
|
+
if (Platform.OS !== 'ios') return 'unsupported';
|
|
115
|
+
|
|
116
|
+
const normalizedParams = Array.from(new Set(props.options));
|
|
117
|
+
return CourierReactNativeModules.iOSForegroundPresentationOptions({
|
|
118
|
+
options: normalizedParams,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Sets the current Apple Push Notification Service (APNS) token
|
|
125
|
+
* using Courier token management apis
|
|
126
|
+
* @example const apnsToken = await Courier.apnsToken
|
|
127
|
+
*/
|
|
128
|
+
get apnsToken(): string | undefined {
|
|
129
|
+
if (Platform.OS !== 'ios') return undefined;
|
|
130
|
+
return CourierReactNativeModules.getApnsToken();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Sets the current Firebase Cloud Messaging (FCM) token
|
|
135
|
+
* using Courier token management apis
|
|
136
|
+
* @example const fcmToken = await Courier.fcmToken
|
|
137
|
+
*/
|
|
138
|
+
get fcmToken(): string | undefined {
|
|
139
|
+
return CourierReactNativeModules.getFcmToken();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Sets the current Firebase Cloud Messaging (FCM) token
|
|
144
|
+
* using Courier token management apis
|
|
145
|
+
* @example await setFcmToken('asdf...asdf')
|
|
146
|
+
*/
|
|
147
|
+
public setFcmToken(props: { token: string }): Promise<void> {
|
|
148
|
+
return CourierReactNativeModules.setFcmToken(props.token);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Gets notification permission status at a system level.
|
|
153
|
+
* @example const permissionStatus = await Courier.getNotificationPermissionStatus()
|
|
154
|
+
*/
|
|
155
|
+
public getNotificationPermissionStatus(): Promise<string> {
|
|
156
|
+
return CourierReactNativeModules.getNotificationPermissionStatus();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Requests notification permission status at a system level.
|
|
161
|
+
* Returns the string associated with the permission status.
|
|
162
|
+
* Will return the current status and will not present a popup
|
|
163
|
+
* if the user has already been asked for permission.
|
|
164
|
+
* @example const permissionStatus = await Courier.requestNotificationPermission()
|
|
165
|
+
*/
|
|
166
|
+
public requestNotificationPermission(): Promise<string> {
|
|
167
|
+
return CourierReactNativeModules.requestNotificationPermission();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @example
|
|
172
|
+
TODO
|
|
173
|
+
* @returns function that can be used to unsubscribe from registered listeners
|
|
174
|
+
*/
|
|
175
|
+
public addPushNotificationListener(props: { onPushNotificationClicked?: (push: any) => void, onPushNotificationDelivered?: (push: any) => void }): CourierPushListener {
|
|
176
|
+
|
|
177
|
+
const pushListener = new CourierPushListener();
|
|
178
|
+
|
|
179
|
+
if (Platform.OS === 'android') {
|
|
180
|
+
|
|
181
|
+
if (props.onPushNotificationClicked) {
|
|
182
|
+
pushListener.onNotificationClickedListener = DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, (event: any) => {
|
|
183
|
+
try {
|
|
184
|
+
props.onPushNotificationClicked!(JSON.parse(event));
|
|
185
|
+
} catch (error) {
|
|
186
|
+
console.log(error);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (props.onPushNotificationDelivered) {
|
|
192
|
+
pushListener.onNotificationDeliveredListener = DeviceEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, (event: any) => {
|
|
193
|
+
try {
|
|
194
|
+
props.onPushNotificationDelivered!(JSON.parse(event));
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.log(error);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (Platform.OS === 'ios') {
|
|
204
|
+
|
|
205
|
+
if (props.onPushNotificationClicked) {
|
|
206
|
+
pushListener.onNotificationClickedListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_CLICKED, (event: any) => {
|
|
207
|
+
try {
|
|
208
|
+
props.onPushNotificationClicked!(JSON.parse(event));
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.log(error);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (props.onPushNotificationDelivered) {
|
|
216
|
+
pushListener.onNotificationDeliveredListener = CourierEventEmitter.addListener(this.PUSH_NOTIFICATION_DELIVERED, (event: any) => {
|
|
217
|
+
try {
|
|
218
|
+
props.onPushNotificationDelivered!(JSON.parse(event));
|
|
219
|
+
} catch (error) {
|
|
220
|
+
console.log(error);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// When listener is registered
|
|
228
|
+
// Attempt to fetch the last message that was clicked
|
|
229
|
+
// This is needed for when the app is killed and the
|
|
230
|
+
// user launched the app by clicking on a notifications
|
|
231
|
+
CourierReactNativeModules.registerPushNotificationClickedOnKilledState();
|
|
232
|
+
|
|
233
|
+
return pushListener
|
|
234
|
+
|
|
235
|
+
}
|
|
236
|
+
|
|
99
237
|
/**
|
|
100
238
|
* Returns the current user id stored in local native storage
|
|
101
239
|
* @example const userId = await Courier.userId
|
|
102
240
|
*/
|
|
103
|
-
get userId():
|
|
104
|
-
return CourierReactNativeModules.getUserId()
|
|
241
|
+
get userId(): string | undefined {
|
|
242
|
+
return CourierReactNativeModules.getUserId() ?? undefined
|
|
105
243
|
}
|
|
106
244
|
|
|
107
245
|
/**
|
|
@@ -124,6 +262,43 @@ class Courier {
|
|
|
124
262
|
return CourierReactNativeModules.signIn(props.accessToken, props.clientKey ?? null, props.userId);
|
|
125
263
|
}
|
|
126
264
|
|
|
265
|
+
/**
|
|
266
|
+
* TODO
|
|
267
|
+
* @param props
|
|
268
|
+
* @returns
|
|
269
|
+
*/
|
|
270
|
+
public signOut(): Promise<void> {
|
|
271
|
+
return CourierReactNativeModules.signOut();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* TODO
|
|
276
|
+
* @param props
|
|
277
|
+
* @returns
|
|
278
|
+
*/
|
|
279
|
+
public addAuthenticationListener(props: { onUserChanged: (userId?: string) => void }): CourierAuthenticationListener {
|
|
280
|
+
|
|
281
|
+
const authListener = new CourierAuthenticationListener();
|
|
282
|
+
|
|
283
|
+
authListener.onUserChanged = CourierEventEmitter.addListener('courierAuthUserChanged', event => {
|
|
284
|
+
props.onUserChanged(event)
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
authListener.listenerId = CourierReactNativeModules.addAuthenticationListener();
|
|
288
|
+
|
|
289
|
+
return authListener;
|
|
290
|
+
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* TODO
|
|
295
|
+
* @param props
|
|
296
|
+
* @returns
|
|
297
|
+
*/
|
|
298
|
+
public removeAuthenticationListener(props: { listenerId: string }): string {
|
|
299
|
+
return CourierReactNativeModules.removeAuthenticationListener(props.listenerId);
|
|
300
|
+
}
|
|
301
|
+
|
|
127
302
|
/**
|
|
128
303
|
* TODO
|
|
129
304
|
* @param props
|
|
@@ -184,7 +359,7 @@ class Courier {
|
|
|
184
359
|
});
|
|
185
360
|
}
|
|
186
361
|
|
|
187
|
-
inboxListener.listenerId = CourierReactNativeModules.addInboxListener(
|
|
362
|
+
inboxListener.listenerId = CourierReactNativeModules.addInboxListener();
|
|
188
363
|
|
|
189
364
|
return inboxListener;
|
|
190
365
|
|
|
@@ -207,6 +382,24 @@ class Courier {
|
|
|
207
382
|
public async refreshInbox(): Promise<void> {
|
|
208
383
|
return CourierReactNativeModules.refreshInbox();
|
|
209
384
|
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* TODO
|
|
388
|
+
* @param props
|
|
389
|
+
* @returns
|
|
390
|
+
*/
|
|
391
|
+
public async fetchNextPageOfMessages(): Promise<InboxMessage[]> {
|
|
392
|
+
return CourierReactNativeModules.fetchNextPageOfMessages();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* TODO
|
|
397
|
+
* @param props
|
|
398
|
+
* @returns
|
|
399
|
+
*/
|
|
400
|
+
public setInboxPaginationLimit(props: { limit: number }): void {
|
|
401
|
+
CourierReactNativeModules.setInboxPaginationLimit(props.limit);
|
|
402
|
+
}
|
|
210
403
|
|
|
211
404
|
}
|
|
212
405
|
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { EmitterSubscription } from "react-native";
|
|
2
|
+
import Courier from "..";
|
|
3
|
+
|
|
4
|
+
export class CourierAuthenticationListener {
|
|
5
|
+
|
|
6
|
+
public listenerId?: string
|
|
7
|
+
public onUserChanged?: EmitterSubscription
|
|
8
|
+
|
|
9
|
+
public remove() {
|
|
10
|
+
|
|
11
|
+
if (this.listenerId) {
|
|
12
|
+
Courier.shared.removeInboxListener({ listenerId: this.listenerId });
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
this.onUserChanged?.remove();
|
|
16
|
+
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { EmitterSubscription } from "react-native";
|
|
2
|
+
|
|
3
|
+
export class CourierPushListener {
|
|
4
|
+
|
|
5
|
+
public onNotificationClickedListener?: EmitterSubscription
|
|
6
|
+
public onNotificationDeliveredListener?: EmitterSubscription
|
|
7
|
+
|
|
8
|
+
public remove() {
|
|
9
|
+
this.onNotificationClickedListener?.remove();
|
|
10
|
+
this.onNotificationDeliveredListener?.remove();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
}
|
|
@@ -5,8 +5,10 @@ import { InboxAction } from "src/models/InboxAction";
|
|
|
5
5
|
import { InboxMessage } from "src/models/InboxMessage";
|
|
6
6
|
|
|
7
7
|
type CourierInboxViewProps = {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
theme?: {
|
|
9
|
+
light?: CourierInboxTheme,
|
|
10
|
+
dark?: CourierInboxTheme
|
|
11
|
+
};
|
|
10
12
|
onClickInboxMessageAtIndex?: (message: InboxMessage, index: number) => void;
|
|
11
13
|
onClickInboxActionForMessageAtIndex?: (action: InboxAction, message: InboxMessage, index: number) => void;
|
|
12
14
|
onScrollInbox?: (offsetY: number, offsetX: number) => void;
|
|
@@ -73,8 +75,7 @@ export const CourierInboxView = (props: CourierInboxViewProps) => {
|
|
|
73
75
|
|
|
74
76
|
return (
|
|
75
77
|
<CourierInbox
|
|
76
|
-
|
|
77
|
-
darkTheme={props.darkTheme}
|
|
78
|
+
theme={props.theme ?? { light: undefined, dark: undefined }}
|
|
78
79
|
onClickInboxMessageAtIndex={onClickInboxMessageAtIndex}
|
|
79
80
|
onClickInboxActionForMessageAtIndex={onClickInboxActionForMessageAtIndex}
|
|
80
81
|
onScrollInbox={onScrollInbox}
|