notifly-sdk 2.2.6 → 2.3.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 (45) hide show
  1. package/README.md +9 -26
  2. package/android/build.gradle +100 -0
  3. package/android/gradle.properties +5 -0
  4. package/android/src/main/AndroidManifest.xml +4 -0
  5. package/android/src/main/java/com/notiflysdk/NotiflySdkModule.kt +105 -0
  6. package/android/src/main/java/com/notiflysdk/NotiflySdkPackage.kt +35 -0
  7. package/android/src/newarch/NotiflySdkSpec.kt +7 -0
  8. package/android/src/oldarch/NotiflySdkSpec.kt +24 -0
  9. package/ios/NotiflySdk-Bridging-Header.h +2 -0
  10. package/ios/NotiflySdk.m +22 -0
  11. package/ios/NotiflySdk.swift +39 -0
  12. package/ios/NotiflySdk.xcodeproj/project.pbxproj +273 -0
  13. package/ios/NotiflySdk.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  14. package/lib/commonjs/NativeNotiflySdk.js +10 -0
  15. package/lib/commonjs/NativeNotiflySdk.js.map +1 -0
  16. package/lib/commonjs/index.js +85 -0
  17. package/lib/commonjs/index.js.map +1 -0
  18. package/lib/commonjs/types.js +2 -0
  19. package/lib/commonjs/types.js.map +1 -0
  20. package/lib/module/NativeNotiflySdk.js +3 -0
  21. package/lib/module/NativeNotiflySdk.js.map +1 -0
  22. package/lib/module/index.js +70 -0
  23. package/lib/module/index.js.map +1 -0
  24. package/lib/module/types.js +2 -0
  25. package/lib/module/types.js.map +1 -0
  26. package/lib/typescript/NativeNotiflySdk.d.ts +15 -0
  27. package/lib/typescript/NativeNotiflySdk.d.ts.map +1 -0
  28. package/lib/typescript/index.d.ts +26 -0
  29. package/lib/typescript/index.d.ts.map +1 -0
  30. package/lib/typescript/types.d.ts +7 -0
  31. package/lib/typescript/types.d.ts.map +1 -0
  32. package/notifly_react_native_sdk.podspec +36 -0
  33. package/package.json +158 -28
  34. package/src/NativeNotiflySdk.ts +25 -0
  35. package/src/index.tsx +105 -0
  36. package/src/types.ts +7 -0
  37. package/.eslintrc.cjs +0 -40
  38. package/.prettierrc.yml +0 -14
  39. package/index.js +0 -190
  40. package/src/auth.js +0 -42
  41. package/src/constant.js +0 -8
  42. package/src/in_app_message.js +0 -258
  43. package/src/log_event.js +0 -185
  44. package/src/user.js +0 -51
  45. package/src/utils.js +0 -145
package/src/log_event.js DELETED
@@ -1,185 +0,0 @@
1
- import { Platform } from 'react-native';
2
- import rnDeviceInfo from 'react-native-device-info';
3
- import AsyncStorage from '@react-native-async-storage/async-storage';
4
- import messaging from '@react-native-firebase/messaging';
5
- import { v5 as uuidv5 } from 'uuid';
6
- import { NAMESPACE, SDK_VERSION } from './constant';
7
- import { getNotiflyUserId } from './utils';
8
- import { getCognitoIdToken } from './auth';
9
-
10
- const NOTIFLY_LOG_EVENT_URL = 'https://12lnng07q2.execute-api.ap-northeast-2.amazonaws.com/prod/records';
11
-
12
- /**
13
- * Logs an event to Notifly for the current user.
14
- *
15
- * @async
16
- * @param {string} eventName - The name of the event to log.
17
- * @param {Object} eventParams - The parameters to include in the event log.
18
- * @param {string[]} [segmentation_event_param_keys=null] - The segmentation event parameter keys.
19
- * @param {boolean} [isInternalEvent=false] - A flag indicating whether the event is for Notifly internal use.
20
- * @returns {Promise<void>} A promise that resolves when the event has been logged, or rejects with an error.
21
- *
22
- * @example
23
- * await logEvent('button_clicked', { 'button_name': 'myButton' });
24
- */
25
- export default async function logEvent(
26
- eventName,
27
- eventParams,
28
- segmentation_event_param_keys = null,
29
- isInternalEvent = false
30
- ) {
31
- if (!eventName) {
32
- console.warn('[Notifly] eventName must be provided.');
33
- return;
34
- }
35
-
36
- try {
37
- const [
38
- cognitoToken,
39
- notiflyUserId,
40
- externalUserId,
41
- prjId,
42
- externalDeviceId,
43
- osVersion,
44
- appVersion,
45
- deviceToken,
46
- ] = await Promise.all([
47
- AsyncStorage.getItem('notiflyCognitoIdToken'),
48
- getNotiflyUserId(),
49
- AsyncStorage.getItem('notiflyExternalUserId'),
50
- AsyncStorage.getItem('notiflyProjectId'),
51
- rnDeviceInfo.getUniqueId(),
52
- rnDeviceInfo.getSystemVersion(),
53
- rnDeviceInfo.getVersion(),
54
- messaging().getToken(),
55
- ]);
56
-
57
- const eventId = uuidv5(`${notiflyUserId}${eventName}${new Date().valueOf()}`, NAMESPACE.EVENTID).replace(
58
- /-/g,
59
- ''
60
- );
61
- const notiflyDeviceId = uuidv5(externalDeviceId, NAMESPACE.DEVICEID).replace(/-/g, '');
62
-
63
- let token = cognitoToken;
64
- if (!token) {
65
- const [userName, password] = await Promise.all([
66
- AsyncStorage.getItem('notiflyUserName'),
67
- AsyncStorage.getItem('notiflyUserPassword'),
68
- ]);
69
- token = await getCognitoIdToken(userName, password);
70
- await AsyncStorage.setItem('notiflyCognitoIdToken', token);
71
- }
72
-
73
- if (!token || !notiflyUserId || !prjId || !eventId || !externalDeviceId || !notiflyDeviceId || !deviceToken) {
74
- const requiredParams = [
75
- 'token',
76
- 'notiflyUserId',
77
- 'prjId',
78
- 'eventId',
79
- 'externalDeviceId',
80
- 'notiflyDeviceId',
81
- 'deviceToken',
82
- ];
83
- const missingParam = requiredParams.find((param) => !eval(param));
84
- throw new Error(`[Notifly] Missing required parameter in logEvent: ${missingParam}`);
85
- }
86
-
87
- const body = _getBodyForLogEvent(
88
- notiflyUserId,
89
- eventId,
90
- eventName,
91
- notiflyDeviceId,
92
- externalDeviceId,
93
- deviceToken,
94
- isInternalEvent,
95
- segmentation_event_param_keys,
96
- prjId,
97
- osVersion,
98
- appVersion,
99
- externalUserId,
100
- eventParams
101
- );
102
- const requestOptions = _getRequestOptionsForLogEvent(token, body);
103
-
104
- const response = await _apiCall(NOTIFLY_LOG_EVENT_URL, requestOptions);
105
- const result = JSON.parse(response);
106
-
107
- // If the token is expired, get a new token and retry the logEvent.
108
- if (result.message == 'The incoming token has expired') {
109
- const [userName, password] = await Promise.all([
110
- AsyncStorage.getItem('notiflyUserName'),
111
- AsyncStorage.getItem('notiflyUserPassword'),
112
- ]);
113
- const newToken = await getCognitoIdToken(userName, password);
114
- await AsyncStorage.setItem('notiflyCognitoIdToken', newToken);
115
- await logEvent(eventName, eventParams, segmentation_event_param_keys, isInternalEvent);
116
- }
117
- } catch (err) {
118
- console.warn('[Notifly] Failed logging the event. Please retry the initialization. ', err);
119
- }
120
- }
121
-
122
- async function _apiCall(apiUrl, requestOptions) {
123
- const result = fetch(apiUrl, requestOptions).then((response) => response.text());
124
- return result;
125
- }
126
-
127
- function _getRequestOptionsForLogEvent(token, body) {
128
- const myHeaders = new Headers();
129
- myHeaders.append('Authorization', token);
130
- myHeaders.append('Content-Type', 'application/json');
131
-
132
- const requestOptions = {
133
- method: 'POST',
134
- headers: myHeaders,
135
- body: body,
136
- redirect: 'follow',
137
- };
138
- return requestOptions;
139
- }
140
-
141
- function _getBodyForLogEvent(
142
- notiflyUserId,
143
- eventId,
144
- eventName,
145
- notiflyDeviceId,
146
- externalDeviceId,
147
- deviceToken,
148
- isInternalEvent,
149
- segmentation_event_param_keys,
150
- prjId,
151
- osVersion,
152
- appVersion,
153
- externalUserId,
154
- eventParams
155
- ) {
156
- const eventData = JSON.stringify({
157
- event_params: eventParams,
158
- id: eventId,
159
- name: eventName,
160
- notifly_user_id: notiflyUserId,
161
- time: parseInt(new Date().valueOf() / 1000),
162
- notifly_device_id: notiflyDeviceId,
163
- external_device_id: externalDeviceId,
164
- device_token: deviceToken,
165
- is_internal_event: isInternalEvent,
166
- segmentation_event_param_keys: segmentation_event_param_keys,
167
- project_id: prjId,
168
- platform: Platform.OS,
169
- os_version: osVersion,
170
- app_version: appVersion,
171
- sdk_version: SDK_VERSION,
172
- sdk_type: "react_native",
173
- external_user_id: externalUserId || undefined,
174
- });
175
-
176
- const body = JSON.stringify({
177
- 'records': [
178
- {
179
- 'data': eventData,
180
- 'partitionKey': notiflyUserId,
181
- },
182
- ],
183
- });
184
- return body;
185
- }
package/src/user.js DELETED
@@ -1,51 +0,0 @@
1
- import AsyncStorage from '@react-native-async-storage/async-storage';
2
- import { getNotiflyUserId } from './utils';
3
- import logEvent from './log_event';
4
-
5
- /**
6
- * Sets user properties for the current user.
7
- *
8
- * @async
9
- * @param {Object} params - An object containing the user properties to set.
10
- * @returns {Promise<void>} A promise that resolves when the user properties have been set, or rejects with an error.
11
- *
12
- * @example
13
- * await setUserProperties({ external_user_id: 'myUserID' });
14
- */
15
- export async function setUserProperties(params) {
16
- try {
17
- if (params.external_user_id) {
18
- const [previousNotiflyUserID, previousExternalUserID] = await Promise.all([
19
- getNotiflyUserId(),
20
- AsyncStorage.getItem('notiflyExternalUserId'),
21
- ]);
22
- await Promise.all([
23
- AsyncStorage.setItem('notiflyExternalUserId', params.external_user_id),
24
- AsyncStorage.removeItem('notiflyUserId'),
25
- ]);
26
- params['previous_notifly_user_id'] = previousNotiflyUserID;
27
- params['previous_external_user_id'] = previousExternalUserID;
28
- }
29
- return await logEvent('set_user_properties', params, null, true);
30
- } catch (err) {
31
- console.warn('[Notifly] Failed to set user properties');
32
- }
33
- }
34
-
35
- /**
36
- * Removes the external user ID and Notifly user ID from storage.
37
- *
38
- * @async
39
- * @returns {Promise<void>} A promise that resolves when the user IDs have been removed, or rejects with an error.
40
- *
41
- * @example
42
- * await removeUserId();
43
- */
44
- export async function removeUserId() {
45
- try {
46
- await Promise.all([AsyncStorage.removeItem('notiflyExternalUserId'), AsyncStorage.removeItem('notiflyUserId')]);
47
- return await logEvent('remove_external_user_id', {}, null, true);
48
- } catch (err) {
49
- console.warn('[Notifly] Failed to remove userID');
50
- }
51
- }
package/src/utils.js DELETED
@@ -1,145 +0,0 @@
1
- import { Platform, Linking } from 'react-native';
2
- import rnDeviceInfo from 'react-native-device-info';
3
- import AsyncStorage from '@react-native-async-storage/async-storage';
4
- import messaging from '@react-native-firebase/messaging';
5
- import { v5 as uuidv5 } from 'uuid';
6
- import { NAMESPACE } from './constant';
7
- import logEvent from './log_event';
8
-
9
- /**
10
- * Gets the Notifly user ID for the current user.
11
- *
12
- * @async
13
- * @returns {Promise<string>} A promise that resolves with the Notifly user ID, or rejects with an error.
14
- *
15
- * @example
16
- * const notiflyUserId = await getNotiflyUserId();
17
- */
18
- export async function getNotiflyUserId() {
19
- const encodedUserId = await AsyncStorage.getItem('notiflyUserId');
20
- if (encodedUserId) {
21
- return encodedUserId;
22
- }
23
- const [prjId, externalUserId] = await Promise.all([
24
- AsyncStorage.getItem('notiflyProjectId'),
25
- AsyncStorage.getItem('notiflyExternalUserId'),
26
- ]);
27
- let notiflyUserId;
28
- if (externalUserId) {
29
- notiflyUserId = uuidv5(`${prjId}${externalUserId}`, NAMESPACE.REGISTERED_USERID).replace(/-/g, '');
30
- } else {
31
- notiflyUserId = uuidv5(`${prjId}${await messaging().getToken()}`, NAMESPACE.UNREGISTERED_USERID).replace(
32
- /-/g,
33
- ''
34
- );
35
- }
36
- return notiflyUserId;
37
- }
38
-
39
- /**
40
- * Logs a 'session_start' event with device information.
41
- *
42
- * @async
43
- * @returns {Promise<void>} A promise that resolves when the event is logged, or rejects with an error.
44
- *
45
- * @example
46
- * await sessionStart();
47
- */
48
- export async function sessionStart() {
49
- const platform = Platform.OS;
50
- const apiLevelPromise = rnDeviceInfo.getApiLevel();
51
- const brandPromise = rnDeviceInfo.getBrand();
52
- const modelPromise = rnDeviceInfo.getModel();
53
- const userAgentPromise = rnDeviceInfo.getUserAgent();
54
- const notifAuthStatusPromise = messaging().hasPermission();
55
-
56
- const [deviceModel, deviceBrand, apiLevel, userAgent, notifAuthStatus] = await Promise.all([
57
- modelPromise,
58
- brandPromise,
59
- apiLevelPromise,
60
- userAgentPromise,
61
- notifAuthStatusPromise,
62
- ]);
63
-
64
- const sessionStartEventParams = {
65
- platform,
66
- type: 'session_start_event',
67
- device_model: deviceModel,
68
- properties: {
69
- device_brand: deviceBrand,
70
- api_level: apiLevel,
71
- user_agent: userAgent,
72
- },
73
- notif_auth_status: notifAuthStatus,
74
- };
75
- await logEvent('session_start', sessionStartEventParams, null, true);
76
- }
77
-
78
- /**
79
- * Handles a click event on a push notification.
80
- *
81
- * @async
82
- * @function
83
- * @param {Object} remoteMessage - The remote message object containing the notification data.
84
- * @returns {Promise<void>} - A promise that resolves when the click event is handled.
85
- *
86
- * @example
87
- * // Usage:
88
- * const remoteMessage = {
89
- * data: {
90
- * link: 'https://example.com',
91
- * campaign_id: '1234'
92
- * }
93
- * };
94
- * await clickHandler(remoteMessage);
95
- */
96
- export async function clickHandler(remoteMessage) {
97
- if (!remoteMessage) {
98
- console.warn('[Notifly] clickHandler receives a null remoteMessage.');
99
- return;
100
- }
101
- if (remoteMessage.data?.notifly_message_type != 'push-notification') {
102
- return;
103
- }
104
-
105
- try {
106
- const { data: { link, campaign_id, notifly_message_id } = {} } = remoteMessage;
107
-
108
- if (link) {
109
- Linking.openURL(link);
110
- }
111
- await logEvent(
112
- 'push_click',
113
- { channel: 'push-notification', type: 'message_event', campaign_id, notifly_message_id, status: 'quit' },
114
- null,
115
- true
116
- );
117
- } catch (err) {
118
- console.warn('[Notifly] custom click handler registration failed.');
119
- }
120
- }
121
-
122
- // React Native does not support Buffer out of the box, so we need to implement our own base64 decoder.
123
- export function base64Decode(input) {
124
- const chars =
125
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
126
- let str = '';
127
- let output = '';
128
-
129
- for (let i = 0; i < input.length; i += 4) {
130
- const encoded1 = chars.indexOf(input[i]);
131
- const encoded2 = chars.indexOf(input[i + 1]);
132
- const encoded3 = chars.indexOf(input[i + 2]);
133
- const encoded4 = chars.indexOf(input[i + 3]);
134
-
135
- const decoded1 = (encoded1 << 2) | (encoded2 >> 4);
136
- const decoded2 = ((encoded2 & 15) << 4) | (encoded3 >> 2);
137
- const decoded3 = ((encoded3 & 3) << 6) | encoded4;
138
-
139
- output += String.fromCharCode(decoded1);
140
- if (encoded3 !== 64) output += String.fromCharCode(decoded2);
141
- if (encoded4 !== 64) output += String.fromCharCode(decoded3);
142
- }
143
-
144
- return output;
145
- }