vibes-react-native 1.0.0 → 1.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.
@@ -1,327 +1,490 @@
1
1
  package com.vibes.push.rn.plugin;
2
2
 
3
3
  import android.app.Application;
4
- import androidx.annotation.NonNull;
5
- import android.content.SharedPreferences;
6
4
  import android.content.pm.ApplicationInfo;
7
5
  import android.content.pm.PackageManager;
8
6
  import android.os.Bundle;
9
- import android.preference.PreferenceManager;
10
7
  import android.util.Log;
11
-
12
- import java.util.Map;
13
-
14
- import com.facebook.react.bridge.Arguments;
15
- import com.facebook.react.bridge.Promise;
16
- import com.facebook.react.bridge.ReactApplicationContext;
17
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
18
- import com.facebook.react.bridge.ReactMethod;
19
- import com.facebook.react.bridge.WritableMap;
8
+ import androidx.annotation.NonNull;
9
+ import com.facebook.react.bridge.*;
20
10
  import com.facebook.react.module.annotations.ReactModule;
21
-
22
11
  import com.google.android.gms.tasks.OnFailureListener;
23
12
  import com.google.android.gms.tasks.OnSuccessListener;
24
13
  import com.google.firebase.iid.FirebaseInstanceId;
25
14
  import com.google.firebase.iid.InstanceIdResult;
15
+ import com.google.gson.Gson;
16
+ import com.vibes.vibes.*;
17
+ import org.json.JSONException;
18
+ import org.json.JSONObject;
26
19
 
27
- import com.vibes.vibes.Credential;
28
- import com.vibes.vibes.Vibes;
29
- import com.vibes.vibes.VibesConfig;
30
- import com.vibes.vibes.VibesListener;
20
+ import java.util.Collection;
31
21
 
32
22
  @ReactModule(name = VibesModule.NAME)
33
23
  public class VibesModule extends ReactContextBaseJavaModule {
34
- public static final String NAME = "Vibes";
35
- public static final String DEVICE_REGISTERED = "DEVICE_REGISTERED";
36
- public static final String TOKEN_REGISTERED = "TOKEN_REGISTERED";
37
- public static final String TAG = "VibesRN";
38
- public static final String TOKEN_KEY = "VibesRN.PushToken";
39
- public static final String DEVICE_ID = "VibesRN.Device_Id";
40
- public static final String VIBES_APPID_KEY = "com.vibes.push.rn.plugin.appId";
41
- public static final String VIBES_APIURL_KEY = "com.vibes.push.rn.plugin.apiUrl";
42
- public static final String REGISTER_DEVICE_ERROR_KEY = "REGISTER_DEVICE_ERROR";
43
- public static final String UNREGISTER_DEVICE_ERROR_KEY = "UNREGISTER_DEVICE_ERROR";
44
- public static final String PLUGIN_ERROR_KEY = "PLUGIN_ERROR";
45
- public static final String REGISTER_PUSH_ERROR_KEY = "REGISTER_PUSH_ERROR";
46
-
47
-
48
- private VibesAppHelper appHelper;
49
-
50
- public VibesModule(ReactApplicationContext reactContext) {
51
- super(reactContext);
52
- appHelper = new VibesAppHelper((Application) reactContext.getApplicationContext());
53
- }
54
-
55
- @Override
56
- @NonNull
57
- public String getName() {
58
- return NAME;
59
- }
60
-
61
- @Override
62
- public void initialize() {
63
- FirebaseInstanceId.getInstance().getInstanceId()
64
- .addOnSuccessListener(
65
- new OnSuccessListener<InstanceIdResult>() {
66
- @Override
67
- public void onSuccess(InstanceIdResult instanceIdResult) {
68
- String instanceToken = instanceIdResult.getToken();
69
- if (instanceToken != null) {
70
- appHelper.saveString(VibesModule.TOKEN_KEY, instanceToken);
71
- Log.d(TAG, "Push token obtained from FirebaseInstanceId --> " + instanceToken);
72
- }
73
- }
74
- }
75
- )
76
- .addOnFailureListener(
77
- new OnFailureListener() {
78
- @Override
79
- public void onFailure(Exception e) {
80
- Log.d(TAG, "Failed to fetch token from FirebaseInstanceId: " + e.getLocalizedMessage());
81
- }
82
- }
83
- );
84
-
85
- if (BuildConfig.DEBUG) Log.d(TAG, "Initializing Vibes SDK");
86
-
87
- String env = null;
88
- String appId = null;
89
- String apiUrl = null;
90
- try {
91
- ApplicationInfo ai = getReactApplicationContext().getPackageManager()
92
- .getApplicationInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
93
- Bundle bundle = ai.metaData;
94
- appId = bundle.getString(VIBES_APPID_KEY);
95
- apiUrl = bundle.getString(VIBES_APIURL_KEY);
96
-
97
- } catch (PackageManager.NameNotFoundException ex) {
98
-
99
- }
100
- if (appId == null || appId.isEmpty()) {
101
- throw new IllegalStateException("No appId provided in manifest under name [" + VIBES_APPID_KEY + "]");
102
- }
103
-
104
- VibesConfig config = null;
105
- if (apiUrl == null || apiUrl.isEmpty()) {
106
- config = new VibesConfig.Builder().setAppId(appId).build();
107
- Log.d(TAG, "Initializing the plugin with appId=[" + appId + "]. Will use default apiUrl");
108
- }else{
109
- config = new VibesConfig.Builder().setApiUrl(apiUrl).setAppId(appId).build();
110
- Log.d(TAG, "Initializing the plugin with appId=[" + appId + "] and apiUrl=[" + apiUrl + "]");
111
- }
112
-
113
- Vibes.initialize(getReactApplicationContext(), config);
114
- this.registerDeviceAtStartup();
115
-
116
- }
117
-
118
- @ReactMethod
119
- public void registerDevice(final Promise promise) {
120
- Log.d(TAG, "Plugin called to register device");
121
- VibesListener<Credential> listener = getRegisterDeviceListener(promise);
122
- Vibes.getInstance().registerDevice(listener);
123
- }
124
-
125
- @ReactMethod
126
- public void getVibesDeviceInfo(final Promise promise) {
127
- Log.d(TAG, "Plugin called to return device info");
128
- WritableMap map = appHelper.getDeviceInfo();
129
- promise.resolve(map);
130
- }
131
-
132
- @ReactMethod
133
- public void registerPush(final Promise promise) {
134
- Log.d(TAG, "Plugin called to register push token with platform");
135
- VibesListener<Void> listener = getRegisterPushListener(promise);
136
- String pushToken = appHelper.getPushToken();
137
- if(pushToken == null){
138
- FirebaseInstanceId.getInstance().getInstanceId()
139
- .addOnSuccessListener(
140
- new OnSuccessListener<InstanceIdResult>() {
141
- @Override
142
- public void onSuccess(InstanceIdResult instanceIdResult) {
143
- String instanceToken = instanceIdResult.getToken();
144
- if (instanceToken == null) {
145
- promise.reject(REGISTER_PUSH_ERROR_KEY,"No push token available for registration yet");
146
- } else {
147
- appHelper.saveString(VibesModule.TOKEN_KEY, instanceToken);
148
- Log.d(TAG, "Push token obtianed from FirebaseInstanceId --> " + instanceToken);
149
- VibesListener<Void> listener = getRegisterPushListener(promise);
150
- registerPush(instanceToken, listener);
151
- }
152
- }
153
- }
154
- )
155
- .addOnFailureListener(
156
- new OnFailureListener() {
157
- @Override
158
- public void onFailure(Exception e) {
159
- Log.d(TAG, "Failed to fetch token from FirebaseInstanceId: "+ e.getLocalizedMessage());
160
- promise.reject(REGISTER_DEVICE_ERROR_KEY, "No push token available for registration yet");
161
- }
162
- }
163
- );
164
- }else{
165
- boolean tokenRegistered = appHelper.getBoolean(VibesModule.TOKEN_REGISTERED, false);
166
- if(tokenRegistered){
167
- promise.resolve("Success");
168
- }else{
169
- registerPush(pushToken, listener);
170
- }
171
- }
172
- }
173
-
174
- @ReactMethod
175
- public void unregisterDevice(final Promise promise) {
176
- VibesListener<Void> listener = new VibesListener<Void>() {
177
- public void onSuccess(Void credential) {
178
- Log.d(TAG, "Unregister device successful");
179
- appHelper.saveString(VibesModule.DEVICE_ID, null);
180
- appHelper.saveBoolean(VibesModule.DEVICE_REGISTERED, false);
181
- promise.resolve("Success");
182
- }
183
-
184
- public void onFailure(String errorText) {
185
- Log.d(TAG, "Unregister device failed");
186
- promise.reject(UNREGISTER_DEVICE_ERROR_KEY, errorText);
187
- }
188
- };
189
- Vibes.getInstance().unregisterDevice(listener);
190
- }
191
-
192
- private void registerDeviceAtStartup() {
193
- VibesListener<Credential> listener = getRegisterDeviceListener(null);
194
- Vibes.getInstance().registerDevice(listener);
195
- }
196
-
197
-
198
- private VibesListener<Credential> getRegisterDeviceListener(final Promise promise) {
199
- return new VibesListener<Credential>() {
200
- public void onSuccess(Credential credential) {
201
- appHelper.saveBoolean(VibesModule.DEVICE_REGISTERED, true);
202
- String deviceId = credential.getDeviceID();
203
- appHelper.saveString(VibesModule.DEVICE_ID, deviceId);
204
- Log.d(TAG, "Device id obtained is --> " + deviceId);
205
- String pushToken = appHelper.getPushToken();
206
- if (pushToken == null) {
207
- Log.d(TAG, "Token not yet available. Skipping registerPush");
208
- } else {
209
- Log.d(TAG, "Token found after registering device. Attempting to register push token");
210
- registerPush(pushToken);
211
- }
212
- WritableMap map = Arguments.createMap();
213
- map.putString("device_id", credential.getDeviceID());
214
- if (promise != null) {
215
- promise.resolve(map);
216
- }
217
- }
218
-
219
- public void onFailure(String errorText) {
220
- Log.e(TAG, "Failure registering device with Vibes Push SDK: " + errorText);
221
- if (promise != null) {
222
- promise.reject(REGISTER_DEVICE_ERROR_KEY, errorText);
223
- }
224
- }
225
- };
226
- }
227
-
228
- private VibesListener<Void> getRegisterPushListener(final Promise promise) {
229
- return new VibesListener<Void>() {
230
- public void onSuccess(Void credential) {
231
- appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, true);
232
- Log.d(TAG, "Push token registration successful");
233
- if(promise !=null){
234
- promise.resolve("Success");
235
- }
236
- }
237
-
238
- public void onFailure(String errorText) {
239
- appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, false);
240
- Log.d(TAG, "Failure registering token with Vibes Push SDK: " + errorText);
241
- if(promise !=null){
242
- promise.reject(REGISTER_DEVICE_ERROR_KEY, errorText);
243
- }
244
- }
245
- };
246
- }
247
-
248
- /**
249
- * To be able to target each device, we need to send the push token generated by the Firebase environment to the
250
- * server-side via the SDK. However, ensure that {@link Vibes#registerDevice()} has been called before this is called.
251
- *
252
- * @param pushToken
253
- */
254
- public static void registerPush(String pushToken) {
255
- Log.d(TAG, "Registering push token with vibes");
256
- VibesListener<Void> listener = new VibesListener<Void>() {
257
- public void onSuccess(Void credential) {
258
- Log.d(TAG, "Push token registration successful");
259
- }
260
-
261
- public void onFailure(String errorText) {
262
- Log.d(TAG, "Failure registering token with Vibes Push SDK: " + errorText);
263
- }
264
- };
265
- registerPush(pushToken, listener);
266
- }
267
-
268
- public static void registerPush(String pushToken, VibesListener<Void> listener) {
269
- Vibes.getInstance().registerPush(pushToken, listener);
270
- }
271
-
272
- /**
273
- * Unregisters a device from receiving further push notifications
274
- */
275
- @ReactMethod
276
- public void unregisterPush(final Promise promise) {
277
- VibesListener<Void> listener = new VibesListener<Void>() {
278
- public void onSuccess(Void credential) {
279
- Log.d(TAG, "Unregister push successful");
280
- appHelper.saveString(VibesModule.TOKEN_KEY, null);
281
- appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, false);
282
- promise.resolve("Success");
283
- }
284
- public void onFailure(String errorText) {
285
- Log.d(TAG, "Unregister push failed");
286
- promise.reject("REGISTER_PUSH_ERROR", errorText);
287
- }
288
- };
289
- Vibes.getInstance().unregisterPush(listener);
290
- }
291
-
292
- /**
293
- * @param externalPersonId The id to associate with the logged-in person.
294
- */
295
- @ReactMethod
296
- public void associatePerson(final String externalPersonId, final Promise promise) {
297
- Log.d(TAG, "Associating Person --> " + externalPersonId);
298
- VibesListener<Void> listener = new VibesListener<Void>() {
299
- public void onSuccess(Void value) {
300
- promise.resolve("Success");;
24
+ public static final String NAME = "Vibes";
25
+ public static final String DEVICE_REGISTERED = "DEVICE_REGISTERED";
26
+ public static final String TOKEN_REGISTERED = "TOKEN_REGISTERED";
27
+ public static final String TAG = "VibesRN";
28
+ public static final String TOKEN_KEY = "VibesRN.PushToken";
29
+ public static final String DEVICE_ID = "VibesRN.Device_Id";
30
+ public static final String VIBES_APPID_KEY = "com.vibes.push.rn.plugin.appId";
31
+ public static final String VIBES_APIURL_KEY = "com.vibes.push.rn.plugin.apiUrl";
32
+ public static final String REGISTER_DEVICE_ERROR_KEY = "REGISTER_DEVICE_ERROR";
33
+ public static final String UNREGISTER_DEVICE_ERROR_KEY = "UNREGISTER_DEVICE_ERROR";
34
+ public static final String PLUGIN_ERROR_KEY = "PLUGIN_ERROR";
35
+ public static final String REGISTER_PUSH_ERROR_KEY = "REGISTER_PUSH_ERROR";
36
+ public static final String GET_PERSON_ERROR = "GET_PERSON_ERROR";
37
+ public static final String FETCH_INBOX_MESSAGES_ERROR = "FETCH_INBOX_MESSAGES_ERROR";
38
+ public static final String FETCH_SINGLE_INBOX_MESSAGE_ERROR = "FETCH_SINGLE_INBOX_MESSAGE_ERROR";
39
+ public static final String PERSON_KEY = "PERSON_KEY";
40
+ public static final String EXTERNAL_PERSON_ID_KEY = "EXTERNAL_PERSON_ID_KEY";
41
+ public static final String MARK_INBOX_MESSAGE_AS_READ_ERROR = "MARK_INBOX_MESSAGE_AS_READ_ERROR";
42
+ public static final String INBOX_MESSAGE_OPEN_ERROR = "INBOX_MESSAGE_OPEN_ERROR";
43
+ public static final String EXPIRE_INBOX_MESSAGE_ERROR = "EXPIRE_INBOX_MESSAGE_ERROR";
44
+
45
+
46
+ private VibesAppHelper appHelper;
47
+
48
+ public VibesModule(ReactApplicationContext reactContext) {
49
+ super(reactContext);
50
+ appHelper = new VibesAppHelper((Application) reactContext.getApplicationContext());
51
+ }
52
+
53
+ @Override
54
+ @NonNull
55
+ public String getName() {
56
+ return NAME;
57
+ }
58
+
59
+ @Override
60
+ public void initialize() {
61
+ FirebaseInstanceId.getInstance().getInstanceId()
62
+ .addOnSuccessListener(
63
+ new OnSuccessListener<InstanceIdResult>() {
64
+ @Override
65
+ public void onSuccess(InstanceIdResult instanceIdResult) {
66
+ String instanceToken = instanceIdResult.getToken();
67
+ if (instanceToken != null) {
68
+ appHelper.saveString(VibesModule.TOKEN_KEY, instanceToken);
69
+ Log.d(TAG, "Push token obtained from FirebaseInstanceId --> " + instanceToken);
301
70
  }
71
+ }
72
+ })
73
+ .addOnFailureListener(
74
+ new OnFailureListener() {
75
+ @Override
76
+ public void onFailure(Exception e) {
77
+ Log.d(TAG, "Failed to fetch token from FirebaseInstanceId: " + e.getLocalizedMessage());
78
+ }
79
+ });
80
+
81
+ if (BuildConfig.DEBUG)
82
+ Log.d(TAG, "Initializing Vibes SDK");
83
+
84
+ String env = null;
85
+ String appId = null;
86
+ String apiUrl = null;
87
+ try {
88
+ ApplicationInfo ai = getReactApplicationContext().getPackageManager()
89
+ .getApplicationInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
90
+ Bundle bundle = ai.metaData;
91
+ appId = bundle.getString(VIBES_APPID_KEY);
92
+ apiUrl = bundle.getString(VIBES_APIURL_KEY);
93
+
94
+ } catch (PackageManager.NameNotFoundException ex) {
302
95
 
303
- public void onFailure(String errorText) {
304
- promise.reject("ASSOCIATE_PERSON_ERROR", errorText);
96
+ }
97
+ if (appId == null || appId.isEmpty()) {
98
+ throw new IllegalStateException("No appId provided in manifest under name [" + VIBES_APPID_KEY + "]");
99
+ }
100
+
101
+ VibesConfig config = null;
102
+ if (apiUrl == null || apiUrl.isEmpty()) {
103
+ config = new VibesConfig.Builder().setAppId(appId).build();
104
+ Log.d(TAG, "Initializing the plugin with appId=[" + appId + "]. Will use default apiUrl");
105
+ } else {
106
+ config = new VibesConfig.Builder().setApiUrl(apiUrl).setAppId(appId).build();
107
+ Log.d(TAG, "Initializing the plugin with appId=[" + appId + "] and apiUrl=[" + apiUrl + "]");
108
+ }
109
+
110
+ Vibes.initialize(getReactApplicationContext(), config);
111
+ this.registerDeviceAtStartup();
112
+
113
+ }
114
+
115
+ @ReactMethod
116
+ public void registerDevice(final Promise promise) {
117
+ Log.d(TAG, "Plugin called to register device");
118
+ VibesListener<Credential> listener = getRegisterDeviceListener(promise);
119
+ Vibes.getInstance().registerDevice(listener);
120
+ }
121
+
122
+ @ReactMethod
123
+ public void getVibesDeviceInfo(final Promise promise) {
124
+ Log.d(TAG, "Plugin called to return device info");
125
+ WritableMap map = appHelper.getDeviceInfo();
126
+ promise.resolve(map);
127
+ }
128
+
129
+ @ReactMethod
130
+ public void registerPush(final Promise promise) {
131
+ Log.d(TAG, "Plugin called to register push token with platform");
132
+ VibesListener<Void> listener = getRegisterPushListener(promise);
133
+ String pushToken = appHelper.getPushToken();
134
+ if (pushToken == null) {
135
+ FirebaseInstanceId.getInstance().getInstanceId()
136
+ .addOnSuccessListener(
137
+ new OnSuccessListener<InstanceIdResult>() {
138
+ @Override
139
+ public void onSuccess(InstanceIdResult instanceIdResult) {
140
+ String instanceToken = instanceIdResult.getToken();
141
+ if (instanceToken == null) {
142
+ promise.reject(REGISTER_PUSH_ERROR_KEY, "No push token available for registration yet");
143
+ } else {
144
+ appHelper.saveString(VibesModule.TOKEN_KEY, instanceToken);
145
+ Log.d(TAG, "Push token obtianed from FirebaseInstanceId --> " + instanceToken);
146
+ VibesListener<Void> listener = getRegisterPushListener(promise);
147
+ registerPush(instanceToken, listener);
148
+ }
305
149
  }
306
- };
307
- this.associatePerson(externalPersonId, listener);
150
+ })
151
+ .addOnFailureListener(
152
+ new OnFailureListener() {
153
+ @Override
154
+ public void onFailure(Exception e) {
155
+ Log.d(TAG, "Failed to fetch token from FirebaseInstanceId: " + e.getLocalizedMessage());
156
+ promise.reject(REGISTER_DEVICE_ERROR_KEY, "No push token available for registration yet");
157
+ }
158
+ });
159
+ } else {
160
+ boolean tokenRegistered = appHelper.getBoolean(VibesModule.TOKEN_REGISTERED, false);
161
+ if (tokenRegistered) {
162
+ promise.resolve("Success");
163
+ } else {
164
+ registerPush(pushToken, listener);
165
+ }
166
+ }
167
+ }
168
+
169
+ @ReactMethod
170
+ public void unregisterDevice(final Promise promise) {
171
+ VibesListener<Void> listener = new VibesListener<Void>() {
172
+ public void onSuccess(Void credential) {
173
+ Log.d(TAG, "Unregister device successful");
174
+ appHelper.saveString(VibesModule.DEVICE_ID, null);
175
+ appHelper.saveBoolean(VibesModule.DEVICE_REGISTERED, false);
176
+ promise.resolve("Success");
177
+ }
178
+
179
+ public void onFailure(String errorText) {
180
+ Log.d(TAG, "Unregister device failed");
181
+ promise.reject(UNREGISTER_DEVICE_ERROR_KEY, errorText);
182
+ }
183
+ };
184
+ Vibes.getInstance().unregisterDevice(listener);
185
+ }
186
+
187
+ private void registerDeviceAtStartup() {
188
+ VibesListener<Credential> listener = getRegisterDeviceListener(null);
189
+ Vibes.getInstance().registerDevice(listener);
190
+ }
191
+
192
+ private VibesListener<Credential> getRegisterDeviceListener(final Promise promise) {
193
+ return new VibesListener<Credential>() {
194
+ public void onSuccess(Credential credential) {
195
+ appHelper.saveBoolean(VibesModule.DEVICE_REGISTERED, true);
196
+ String deviceId = credential.getDeviceID();
197
+ appHelper.saveString(VibesModule.DEVICE_ID, deviceId);
198
+ Log.d(TAG, "Device id obtained is --> " + deviceId);
199
+ String pushToken = appHelper.getPushToken();
200
+ if (pushToken == null) {
201
+ Log.d(TAG, "Token not yet available. Skipping registerPush");
202
+ } else {
203
+ Log.d(TAG, "Token found after registering device. Attempting to register push token");
204
+ registerPush(pushToken);
205
+ }
206
+ WritableMap map = Arguments.createMap();
207
+ map.putString("device_id", credential.getDeviceID());
208
+ if (promise != null) {
209
+ promise.resolve(map);
210
+ }
211
+ }
212
+
213
+ public void onFailure(String errorText) {
214
+ Log.e(TAG, "Failure registering device with Vibes Push SDK: " + errorText);
215
+ if (promise != null) {
216
+ promise.reject(REGISTER_DEVICE_ERROR_KEY, errorText);
217
+ }
218
+ }
219
+ };
220
+ }
221
+
222
+ private VibesListener<Void> getRegisterPushListener(final Promise promise) {
223
+ return new VibesListener<Void>() {
224
+ public void onSuccess(Void credential) {
225
+ appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, true);
226
+ Log.d(TAG, "Push token registration successful");
227
+ if (promise != null) {
228
+ promise.resolve("Success");
229
+ }
230
+ }
231
+
232
+ public void onFailure(String errorText) {
233
+ appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, false);
234
+ Log.d(TAG, "Failure registering token with Vibes Push SDK: " + errorText);
235
+ if (promise != null) {
236
+ promise.reject(REGISTER_DEVICE_ERROR_KEY, errorText);
237
+ }
238
+ }
239
+ };
240
+ }
241
+
242
+ /**
243
+ * To be able to target each device, we need to send the push token generated by
244
+ * the Firebase environment to the
245
+ * server-side via the SDK. However, ensure that {@link Vibes#registerDevice()}
246
+ * has been called before this is called.
247
+ *
248
+ * @param pushToken
249
+ */
250
+ public static void registerPush(String pushToken) {
251
+ Log.d(TAG, "Registering push token with vibes");
252
+ VibesListener<Void> listener = new VibesListener<Void>() {
253
+ public void onSuccess(Void credential) {
254
+ Log.d(TAG, "Push token registration successful");
255
+ }
256
+
257
+ public void onFailure(String errorText) {
258
+ Log.d(TAG, "Failure registering token with Vibes Push SDK: " + errorText);
259
+ }
260
+ };
261
+ registerPush(pushToken, listener);
262
+ }
263
+
264
+ public static void registerPush(String pushToken, VibesListener<Void> listener) {
265
+ Vibes.getInstance().registerPush(pushToken, listener);
266
+ }
267
+
268
+ /**
269
+ * Unregisters a device from receiving further push notifications
270
+ */
271
+ @ReactMethod
272
+ public void unregisterPush(final Promise promise) {
273
+ VibesListener<Void> listener = new VibesListener<Void>() {
274
+ public void onSuccess(Void credential) {
275
+ Log.d(TAG, "Unregister push successful");
276
+ appHelper.saveString(VibesModule.TOKEN_KEY, null);
277
+ appHelper.saveBoolean(VibesModule.TOKEN_REGISTERED, false);
278
+ promise.resolve("Success");
279
+ }
280
+
281
+ public void onFailure(String errorText) {
282
+ Log.d(TAG, "Unregister push failed");
283
+ promise.reject("REGISTER_PUSH_ERROR", errorText);
284
+ }
285
+ };
286
+ Vibes.getInstance().unregisterPush(listener);
287
+ }
288
+
289
+ /**
290
+ * @param externalPersonId The id to associate with the logged-in person.
291
+ */
292
+ @ReactMethod
293
+ public void associatePerson(final String externalPersonId, final Promise promise) {
294
+ Log.d(TAG, "Associating Person --> " + externalPersonId);
295
+ VibesListener<Void> listener = new VibesListener<Void>() {
296
+ public void onSuccess(Void value) {
297
+ promise.resolve("Success");
298
+ ;
299
+ }
300
+
301
+ public void onFailure(String errorText) {
302
+ promise.reject("ASSOCIATE_PERSON_ERROR", errorText);
303
+ }
304
+ };
305
+ this.associatePerson(externalPersonId, listener);
306
+ }
307
+
308
+ private void associatePerson(String externalPersonId, VibesListener<Void> listener) {
309
+ Vibes.getInstance().associatePerson(externalPersonId, listener);
310
+ }
311
+
312
+ /**
313
+ * Fetches details of a person associated with device
314
+ */
315
+ @ReactMethod
316
+ public void getPerson(final Promise promise) {
317
+ VibesListener<Person> listener = new VibesListener<Person>() {
318
+ public void onSuccess(Person value) {
319
+ Log.d(TAG, "Get Person Successful");
320
+ appHelper.saveString(VibesModule.PERSON_KEY, value.getPersonKey());
321
+ appHelper.saveString(VibesModule.EXTERNAL_PERSON_ID_KEY, value.getExternalPersonId());
322
+
323
+ WritableMap map = Arguments.createMap();
324
+ map.putString("person_key", value.getPersonKey());
325
+ map.putString("external_person_id", value.getExternalPersonId());
326
+ if (promise != null) {
327
+ promise.resolve(map);
328
+ }
329
+ }
330
+
331
+ public void onFailure(String errorText) {
332
+ Log.d(TAG, "Get Person Failed failed");
333
+ promise.reject(GET_PERSON_ERROR, errorText);
334
+ }
335
+ };
336
+ Vibes.getInstance().getPerson(listener);
337
+ }
338
+
339
+ /**
340
+ * React Native Plugin Method that updates client device information with Vibes.
341
+ *
342
+ * @param updateCredential a boolean indicating if it's a token update or device
343
+ * info update. Specify false if not certain
344
+ * @param latitude client device latitude
345
+ * @param longitude client device longitude
346
+ */
347
+ @ReactMethod
348
+ public void updateDevice(final boolean updateCredential, Double latitude, Double longitude, final Promise promise) {
349
+ Log.d(TAG, "Plugin called to update");
350
+ VibesListener<Credential> listener = getRegisterDeviceListener(promise);
351
+ Vibes.getInstance().updateDevice(updateCredential, latitude, longitude, listener);
352
+ }
353
+
354
+ @ReactMethod
355
+ public void fetchInboxMessages(final Promise promise) {
356
+ VibesListener<Collection<InboxMessage>> listener = new VibesListener<Collection<InboxMessage>>() {
357
+ public void onSuccess(Collection<InboxMessage> values) {
358
+ Log.d(TAG, "Fetch inbox messages success");
359
+ Gson gson = new Gson();
360
+ WritableArray array = Arguments.createArray();
361
+ for (InboxMessage message : values) {
362
+ try {
363
+ JSONObject jsonObject = new JSONObject(gson.toJson(message));
364
+ WritableMap writableMap = appHelper.convertJsonToMap(jsonObject);
365
+ array.pushMap(writableMap);
366
+ } catch (JSONException e) {
367
+ Log.e(TAG, e.getLocalizedMessage());
368
+ }
369
+ }
370
+ if (promise != null) {
371
+ promise.resolve(array);
372
+ }
373
+ }
374
+
375
+ public void onFailure(String errorText) {
376
+ Log.d(TAG, "Fetch inbox messages failed: " + errorText);
377
+ promise.reject(FETCH_INBOX_MESSAGES_ERROR, errorText);
378
+ }
379
+ };
380
+ Vibes.getInstance().fetchInboxMessages(listener);
381
+ }
382
+
383
+ @ReactMethod
384
+ public void fetchInboxMessage(final String messageUid, final Promise promise) {
385
+ VibesListener<InboxMessage> listener = new VibesListener<InboxMessage>() {
386
+ public void onSuccess(InboxMessage message) {
387
+ Log.d(TAG, "Fetch single inbox message success");
388
+ Gson gson = new Gson();
389
+ try {
390
+ JSONObject jsonObject = new JSONObject(gson.toJson(message));
391
+ WritableMap writableMap = appHelper.convertJsonToMap(jsonObject);
392
+ if (promise != null) {
393
+ promise.resolve(writableMap);
394
+ }
395
+ } catch (JSONException e) {
396
+ Log.e(TAG, e.getLocalizedMessage());
397
+ promise.reject(FETCH_SINGLE_INBOX_MESSAGE_ERROR, e.getLocalizedMessage());
398
+ }
399
+ }
400
+
401
+ public void onFailure(String errorText) {
402
+ Log.d(TAG, "Fetch single inbox message failed: " + errorText);
403
+ promise.reject(FETCH_SINGLE_INBOX_MESSAGE_ERROR, errorText);
404
+ }
405
+ };
406
+ Vibes.getInstance().fetchInboxMessage(messageUid, listener);
407
+ }
408
+
409
+ @ReactMethod
410
+ public void markInboxMessageAsRead(final String messageUid, final Promise promise) {
411
+ VibesListener<InboxMessage> listener = new VibesListener<InboxMessage>() {
412
+ public void onSuccess(InboxMessage message) {
413
+ Log.d(TAG, "Mark inbox message as read");
414
+ Gson gson = new Gson();
415
+ try {
416
+ JSONObject jsonObject = new JSONObject(gson.toJson(message));
417
+ WritableMap writableMap = appHelper.convertJsonToMap(jsonObject);
418
+ if (promise != null) {
419
+ promise.resolve(writableMap);
420
+ }
421
+ } catch (JSONException e) {
422
+ Log.e(TAG, e.getLocalizedMessage());
423
+ promise.reject(MARK_INBOX_MESSAGE_AS_READ_ERROR, e.getLocalizedMessage());
424
+ }
425
+ }
426
+
427
+ public void onFailure(String errorText) {
428
+ Log.d(TAG, "Marking inbox message as read failed: " + errorText);
429
+ promise.reject(MARK_INBOX_MESSAGE_AS_READ_ERROR, errorText);
430
+ }
431
+ };
432
+ Vibes.getInstance().markInboxMessageAsRead(messageUid, listener);
433
+ }
434
+
435
+ @ReactMethod
436
+ public void expireInboxMessage(final String messageUid, final Promise promise) {
437
+ VibesListener<InboxMessage> listener = new VibesListener<InboxMessage>() {
438
+ public void onSuccess(InboxMessage message) {
439
+ Log.d(TAG, "Expiring inbox message successful");
440
+ Gson gson = new Gson();
441
+ try {
442
+ JSONObject jsonObject = new JSONObject(gson.toJson(message));
443
+ WritableMap writableMap = appHelper.convertJsonToMap(jsonObject);
444
+ if (promise != null) {
445
+ promise.resolve(writableMap);
446
+ }
447
+ } catch (JSONException e) {
448
+ Log.e(TAG, e.getLocalizedMessage());
449
+ promise.reject(EXPIRE_INBOX_MESSAGE_ERROR, e.getLocalizedMessage());
450
+ }
451
+ }
452
+
453
+ public void onFailure(String errorText) {
454
+ Log.d(TAG, "Expiring inbox message failed: " + errorText);
455
+ promise.reject(EXPIRE_INBOX_MESSAGE_ERROR, errorText);
456
+ }
457
+ };
458
+ Vibes.getInstance().expireInboxMessage(messageUid, listener);
459
+ }
460
+
461
+ @ReactMethod
462
+ public void onInboxMessageOpen(ReadableMap readableMap, final Promise promise) {
463
+ Log.d(TAG, "Recording an inbox_open event");
464
+ Gson gson = new Gson();
465
+ try {
466
+ JSONObject jsonObject = appHelper.convertMapToJson(readableMap);
467
+ InboxMessage inboxMessage = gson.fromJson(jsonObject.toString(), InboxMessage.class);
468
+ Vibes.getInstance().onInboxMessageOpen(inboxMessage);
469
+ if (promise != null) {
470
+ Log.d(TAG, "Success recording an inbox_open event");
471
+ promise.resolve("");
472
+ }
473
+ } catch (Exception e) {
474
+ Log.e(TAG, "Error recording inbox_open event: " + e.getLocalizedMessage());
475
+ promise.reject(INBOX_MESSAGE_OPEN_ERROR, e.getLocalizedMessage());
308
476
  }
309
477
 
310
- private void associatePerson(String externalPersonId, VibesListener<Void> listener) {
311
- Vibes.getInstance().associatePerson(externalPersonId, listener);
478
+ }
479
+
480
+ @ReactMethod
481
+ public void onInboxMessagesFetched(final Promise promise) {
482
+ Log.d(TAG, "Recording an inbox_fetch event");
483
+ Vibes.getInstance().onInboxMessagesFetched();
484
+ if (promise != null) {
485
+ Log.d(TAG, "Success recording an inbox_fetch event");
486
+ promise.resolve("");
312
487
  }
488
+ }
313
489
 
314
- /**
315
- * React Native Plugin Method that updates client device information with Vibes.
316
- *
317
- * @param updateCredential a boolean indicating if it's a token update or device info update. Specify false if not certain
318
- * @param latitude client device latitude
319
- * @param longitude client device longitude
320
- */
321
- @ReactMethod
322
- public void updateDevice(final boolean updateCredential, Double latitude, Double longitude, final Promise promise) {
323
- Log.d(TAG, "Plugin called to update");
324
- VibesListener<Credential> listener = getRegisterDeviceListener(promise);
325
- Vibes.getInstance().updateDevice(updateCredential, latitude, longitude, listener);
326
- }
327
490
  }