notifly-sdk 1.0.4 → 1.0.6
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/.prettierrc.yml +14 -0
- package/index.js +43 -74
- package/package.json +1 -2
package/.prettierrc.yml
ADDED
package/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import messaging from "@react-native-firebase/messaging";
|
|
2
|
-
import notifee, { AndroidImportance } from "@notifee/react-native";
|
|
3
2
|
import { Linking, Platform } from 'react-native';
|
|
4
3
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
5
4
|
import rnDeviceInfo from 'react-native-device-info';
|
|
@@ -10,9 +9,20 @@ exports.trackEvent = logEvent;
|
|
|
10
9
|
|
|
11
10
|
exports.setUserProperties = _setUserProperties
|
|
12
11
|
|
|
13
|
-
exports.setUserId = async
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
exports.setUserId = async function (userID) {
|
|
13
|
+
try {
|
|
14
|
+
if (userID) {
|
|
15
|
+
await _setUserProperties({
|
|
16
|
+
external_user_id: userID,
|
|
17
|
+
})
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
await _removeUserId();
|
|
21
|
+
return;
|
|
22
|
+
} catch (err) {
|
|
23
|
+
console.warn('[Notifly] setUserId Failed.');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
16
26
|
|
|
17
27
|
exports.sessionStart = async function () {
|
|
18
28
|
const apiLevel = rnDeviceInfo.getApiLevel(); // only android
|
|
@@ -48,82 +58,19 @@ exports.initialize = async function (prjId, userName, password) {
|
|
|
48
58
|
Linking.openURL(remoteMessage.notification?.android?.link);
|
|
49
59
|
}
|
|
50
60
|
await logEvent("push_click", { "campaign_id": remoteMessage.data.campaign_id, "status": "quit" }, null, true);
|
|
51
|
-
await logEvent("push_delivered", { "campaign_id": remoteMessage.data.campaign_id, "status": "quit" }, null, true);
|
|
52
|
-
|
|
53
61
|
}
|
|
54
62
|
});
|
|
55
63
|
|
|
56
|
-
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
|
|
57
|
-
await logEvent("push_delivered", { "campaign_id": remoteMessage.data.campaign_id, "status": "background" }, null, true);
|
|
58
|
-
return
|
|
59
|
-
})
|
|
60
|
-
|
|
61
64
|
await Promise.all([
|
|
62
65
|
AsyncStorage.setItem('notiflyProjectId', prjId),
|
|
63
66
|
AsyncStorage.setItem('notiflyUserName', userName),
|
|
64
67
|
AsyncStorage.setItem('notiflyUserPassword', password),
|
|
65
68
|
])
|
|
66
|
-
|
|
67
|
-
notifee.requestPermission()
|
|
68
|
-
if (Platform.OS === 'android') {
|
|
69
|
-
notifee.createChannel({
|
|
70
|
-
id: "foreground",
|
|
71
|
-
name: "foreground",
|
|
72
|
-
importance: AndroidImportance.HIGH,
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
notifee.onBackgroundEvent(async ({ type, detail }) => {
|
|
76
|
-
const { notification } = detail;
|
|
77
|
-
const campaign_id = notification.data.campaign_id;
|
|
78
|
-
if (!campaign_id) {
|
|
79
|
-
return
|
|
80
|
-
}
|
|
81
|
-
switch (type) {
|
|
82
|
-
case 3:
|
|
83
|
-
try {
|
|
84
|
-
await logEvent("push_delivered", { "campaign_id": campaign_id, "status": "background-notifee" }, null, true);
|
|
85
|
-
} catch (err) {
|
|
86
|
-
console.warn('[Notifly]: ', err);
|
|
87
|
-
}
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
69
|
} catch (err) {
|
|
92
70
|
console.warn("[Notifly]: ", err);
|
|
93
71
|
}
|
|
94
72
|
}
|
|
95
73
|
|
|
96
|
-
exports.foregroundSetting = function () {
|
|
97
|
-
messaging().onMessage(async remoteMessage => {
|
|
98
|
-
await notifee.displayNotification({
|
|
99
|
-
title: remoteMessage.notification.title,
|
|
100
|
-
body: remoteMessage.notification.body,
|
|
101
|
-
data: remoteMessage.data,
|
|
102
|
-
android: {
|
|
103
|
-
channelId: "foreground",
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
await logEvent("push_delivered", { "campaign_id": remoteMessage.data.campaign_id, "status": "foreground" }, null, true);
|
|
107
|
-
});
|
|
108
|
-
notifee.onForegroundEvent(async ({ type, detail }) => {
|
|
109
|
-
const { notification } = detail;
|
|
110
|
-
const campaign_id = notification.data.campaign_id;
|
|
111
|
-
if (!campaign_id) {
|
|
112
|
-
return
|
|
113
|
-
}
|
|
114
|
-
if (type == 1) {
|
|
115
|
-
try {
|
|
116
|
-
if (notification.data.link) {
|
|
117
|
-
Linking.openURL(notification.data.link)
|
|
118
|
-
}
|
|
119
|
-
await logEvent("push_click", { "campaign_id": campaign_id, "status": "foreground" }, null, true);
|
|
120
|
-
} catch (err) {
|
|
121
|
-
console.warn('[Notifly]: ', err);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
74
|
async function getCognitoIdToken(userName, password) {
|
|
128
75
|
const myHeaders = new Headers();
|
|
129
76
|
myHeaders.append("X-Amz-Target", "AWSCognitoIdentityProviderService.InitiateAuth");
|
|
@@ -165,18 +112,40 @@ async function _getNotiflyUserId() {
|
|
|
165
112
|
}
|
|
166
113
|
|
|
167
114
|
async function _setUserProperties(params) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
115
|
+
try {
|
|
116
|
+
if (params.external_user_id) {
|
|
117
|
+
const [previousNotiflyUserID, previousExternalUserID] = await Promise.all([
|
|
118
|
+
_getNotiflyUserId(),
|
|
119
|
+
AsyncStorage.getItem('notiflyExternalUserId')
|
|
120
|
+
]);
|
|
121
|
+
params['previous_notifly_user_id'] = previousNotiflyUserID
|
|
122
|
+
params['previous_external_user_id'] = previousExternalUserID
|
|
123
|
+
await Promise.all([
|
|
124
|
+
AsyncStorage.setItem('notiflyExternalUserId', params.external_user_id),
|
|
125
|
+
AsyncStorage.removeItem('notiflyUserId'),
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
return (await logEvent("set_user_properties", params, null, true));
|
|
129
|
+
} catch (err) {
|
|
130
|
+
console.warn('[Notifly] Failed to remove userID');
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function _removeUserId() {
|
|
135
|
+
try {
|
|
136
|
+
await Promise.all([AsyncStorage.removeItem('notiflyExternalUserId'), AsyncStorage.removeItem('notiflyUserId')]);
|
|
137
|
+
return await logEvent('remove_external_user_id', {}, null, true);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
console.warn('[Notifly] Failed to remove userID');
|
|
174
140
|
}
|
|
175
|
-
return (await logEvent("set_user_properties", params, null, true));
|
|
176
141
|
}
|
|
177
142
|
|
|
178
143
|
async function logEvent(eventName, eventParams, segmentation_event_param_keys = null, isInternalEvent = false) {
|
|
179
144
|
try {
|
|
145
|
+
if (!eventName) {
|
|
146
|
+
console.warn('[Notifly]event_name must be provided.');
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
180
149
|
let token;
|
|
181
150
|
const [savedCognitoToken, notiflyUserId, externalUserId, prjId, externalDeviceId, osVersion, appVersion] = await Promise.all([
|
|
182
151
|
AsyncStorage.getItem('notiflyCognitoIdToken'),
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "notifly-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"@notifee/react-native": "^7.3.0",
|
|
11
10
|
"@react-native-async-storage/async-storage": "^1.17.11",
|
|
12
11
|
"@react-native-firebase/app": "^16.4.3",
|
|
13
12
|
"@react-native-firebase/messaging": "^16.4.3",
|