vibes-react-native 1.0.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 (31) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +280 -0
  3. package/android/build.gradle +61 -0
  4. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  5. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  6. package/android/gradlew +185 -0
  7. package/android/gradlew.bat +89 -0
  8. package/android/src/main/AndroidManifest.xml +28 -0
  9. package/android/src/main/java/com/vibes/push/rn/plugin/PushEvtEmitter.java +65 -0
  10. package/android/src/main/java/com/vibes/push/rn/plugin/VibesAppHelper.java +100 -0
  11. package/android/src/main/java/com/vibes/push/rn/plugin/VibesModule.java +327 -0
  12. package/android/src/main/java/com/vibes/push/rn/plugin/VibesPackage.java +28 -0
  13. package/android/src/main/java/com/vibes/push/rn/plugin/notifications/Fms.java +116 -0
  14. package/android/src/main/java/com/vibes/push/rn/plugin/notifications/PayloadWrapper.java +23 -0
  15. package/android/src/main/java/com/vibes/push/rn/plugin/notifications/VibesPushReceiver.java +60 -0
  16. package/ios/Configuration.swift +68 -0
  17. package/ios/Notifications.swift +72 -0
  18. package/ios/Vibes-Bridging-Header.h +2 -0
  19. package/ios/Vibes.m +31 -0
  20. package/ios/Vibes.swift +236 -0
  21. package/ios/Vibes.xcodeproj/project.pbxproj +293 -0
  22. package/ios/VibesClient.swift +35 -0
  23. package/ios/VibesPluginLogger.swift +160 -0
  24. package/lib/commonjs/index.js +53 -0
  25. package/lib/commonjs/index.js.map +1 -0
  26. package/lib/module/index.js +31 -0
  27. package/lib/module/index.js.map +1 -0
  28. package/lib/typescript/index.d.ts +8 -0
  29. package/package.json +149 -0
  30. package/src/index.tsx +48 -0
  31. package/vibes-react-native.podspec +20 -0
@@ -0,0 +1,65 @@
1
+ package com.vibes.push.rn.plugin;
2
+
3
+ import android.app.Application;
4
+ import android.os.Build;
5
+ import android.os.Bundle;
6
+ import android.util.Log;
7
+
8
+ import com.facebook.react.bridge.Arguments;
9
+ import com.facebook.react.bridge.ReactContext;
10
+ import com.facebook.react.bridge.WritableMap;
11
+ import com.facebook.react.bridge.WritableNativeMap;
12
+ import com.facebook.react.modules.core.DeviceEventManagerModule;
13
+ import com.vibes.vibes.PushPayloadParser;
14
+ import com.vibes.vibes.Vibes;
15
+
16
+ import org.json.JSONException;
17
+ import org.json.JSONObject;
18
+
19
+ import java.util.Map;
20
+ import java.util.Set;
21
+
22
+ /**
23
+ * Component for emitting events to the Javascript layer.
24
+ */
25
+ public class PushEvtEmitter {
26
+ private ReactContext mReactContext;
27
+ private VibesAppHelper appHelper;
28
+
29
+ public PushEvtEmitter(ReactContext reactContext) {
30
+ mReactContext = reactContext;
31
+ appHelper = new VibesAppHelper((Application) reactContext.getApplicationContext());
32
+ }
33
+
34
+ void sendEvent(String eventName, Object params) {
35
+ if (mReactContext.hasActiveCatalystInstance()) {
36
+ mReactContext
37
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
38
+ .emit(eventName, params);
39
+ }
40
+ }
41
+
42
+ public void notifyPushReceived(PushPayloadParser bundle) {
43
+ WritableMap payload = convertMap(bundle.getMap());
44
+ WritableMap params = Arguments.createMap();
45
+ params.putMap("payload", payload);
46
+ sendEvent("pushReceived", params);
47
+ }
48
+
49
+ public void notifyPushOpened(PushPayloadParser bundle) {
50
+ appHelper.invokeApp();
51
+ WritableMap payload = convertMap(bundle.getMap());
52
+ WritableMap params = Arguments.createMap();
53
+ params.putMap("payload", payload);
54
+ sendEvent("pushOpened", params);
55
+ }
56
+
57
+ public static WritableMap convertMap(Map<String, String> data) {
58
+ String jsonString = null;
59
+ WritableMap map = new WritableNativeMap();
60
+ for (Map.Entry<String, String> entry : data.entrySet()) {
61
+ map.putString(entry.getKey(), entry.getValue());
62
+ }
63
+ return map;
64
+ }
65
+ }
@@ -0,0 +1,100 @@
1
+ package com.vibes.push.rn.plugin;
2
+
3
+ import android.app.Application;
4
+ import android.content.Context;
5
+ import android.content.Intent;
6
+ import android.util.Log;
7
+ import com.facebook.react.bridge.Arguments;
8
+ import com.facebook.react.bridge.WritableMap;
9
+
10
+ import android.content.SharedPreferences;
11
+ import android.preference.PreferenceManager;
12
+
13
+ import static com.vibes.push.rn.plugin.VibesModule.TAG;
14
+
15
+ /**
16
+ * Utility class for obtaining items from the ApplicationContext
17
+ */
18
+ public class VibesAppHelper{
19
+ private Context context;
20
+
21
+ public VibesAppHelper(Application context){
22
+ this.context = context;
23
+ }
24
+
25
+ /**
26
+ * Returns the push token already stored in shared preferences after Firebase runtime was initialized.
27
+ * @return
28
+ */
29
+ public String getPushToken(){
30
+ return getSharedPreferences().getString(VibesModule.TOKEN_KEY, null);
31
+ }
32
+
33
+ /**
34
+ * Returns the Vibes device id already stored in shared preferences after Vibes SDK was initialized.
35
+ * @return
36
+ */
37
+ public String getDeviceId(){
38
+ return getSharedPreferences().getString(VibesModule.DEVICE_ID, null);
39
+ }
40
+
41
+ /**
42
+ * Returns a Map containing items for <code>device_id</code> and <code>push_token</code> keys to enable quick display.
43
+ *
44
+ * @return
45
+ */
46
+ public WritableMap getDeviceInfo(){
47
+ WritableMap map = Arguments.createMap();
48
+ map.putString("device_id", getDeviceId());
49
+ map.putString("push_token", getPushToken());
50
+ return map;
51
+ }
52
+
53
+ public void saveString(String key, String value){
54
+ SharedPreferences.Editor editor = getSharedPreferences().edit();
55
+ editor.putString(key, value);
56
+ editor.apply();
57
+ }
58
+
59
+ public void saveBoolean(String key, boolean value){
60
+ SharedPreferences.Editor editor = getSharedPreferences().edit();
61
+ editor.putBoolean(key, value);
62
+ editor.apply();
63
+ }
64
+
65
+ public boolean getBoolean(String key, boolean alternative){
66
+ return getSharedPreferences().getBoolean(key, alternative);
67
+ }
68
+
69
+ public String getString(String key, String alternative){
70
+ return getSharedPreferences().getString(key, alternative);
71
+ }
72
+
73
+ private SharedPreferences getSharedPreferences(){
74
+ return PreferenceManager.getDefaultSharedPreferences(this.context);
75
+ }
76
+
77
+ private Class getMainActivityClass() {
78
+ String packageName = context.getPackageName();
79
+ Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
80
+ String className = launchIntent.getComponent().getClassName();
81
+ try {
82
+ return Class.forName(className);
83
+ } catch (ClassNotFoundException e) {
84
+ e.printStackTrace();
85
+ return null;
86
+ }
87
+ }
88
+
89
+ public void invokeApp() {
90
+ try {
91
+ Class<?> activityClass = getMainActivityClass();
92
+ Intent activityIntent = new Intent(context, activityClass);
93
+ activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
94
+ context.startActivity(activityIntent);
95
+ } catch(Exception e) {
96
+ Log.e(TAG, "Class not found", e);
97
+ return;
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,327 @@
1
+ package com.vibes.push.rn.plugin;
2
+
3
+ import android.app.Application;
4
+ import androidx.annotation.NonNull;
5
+ import android.content.SharedPreferences;
6
+ import android.content.pm.ApplicationInfo;
7
+ import android.content.pm.PackageManager;
8
+ import android.os.Bundle;
9
+ import android.preference.PreferenceManager;
10
+ 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;
20
+ import com.facebook.react.module.annotations.ReactModule;
21
+
22
+ import com.google.android.gms.tasks.OnFailureListener;
23
+ import com.google.android.gms.tasks.OnSuccessListener;
24
+ import com.google.firebase.iid.FirebaseInstanceId;
25
+ import com.google.firebase.iid.InstanceIdResult;
26
+
27
+ import com.vibes.vibes.Credential;
28
+ import com.vibes.vibes.Vibes;
29
+ import com.vibes.vibes.VibesConfig;
30
+ import com.vibes.vibes.VibesListener;
31
+
32
+ @ReactModule(name = VibesModule.NAME)
33
+ 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");;
301
+ }
302
+
303
+ public void onFailure(String errorText) {
304
+ promise.reject("ASSOCIATE_PERSON_ERROR", errorText);
305
+ }
306
+ };
307
+ this.associatePerson(externalPersonId, listener);
308
+ }
309
+
310
+ private void associatePerson(String externalPersonId, VibesListener<Void> listener) {
311
+ Vibes.getInstance().associatePerson(externalPersonId, listener);
312
+ }
313
+
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
+ }
@@ -0,0 +1,28 @@
1
+ package com.vibes.push.rn.plugin;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ import com.facebook.react.ReactPackage;
6
+ import com.facebook.react.bridge.NativeModule;
7
+ import com.facebook.react.bridge.ReactApplicationContext;
8
+ import com.facebook.react.uimanager.ViewManager;
9
+
10
+ import java.util.ArrayList;
11
+ import java.util.Collections;
12
+ import java.util.List;
13
+
14
+ public class VibesPackage implements ReactPackage {
15
+ @NonNull
16
+ @Override
17
+ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18
+ List<NativeModule> modules = new ArrayList<>();
19
+ modules.add(new VibesModule(reactContext));
20
+ return modules;
21
+ }
22
+
23
+ @NonNull
24
+ @Override
25
+ public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
26
+ return Collections.emptyList();
27
+ }
28
+ }
@@ -0,0 +1,116 @@
1
+ package com.vibes.push.rn.plugin.notifications;
2
+
3
+ import android.content.Intent;
4
+ import android.content.SharedPreferences;
5
+ import android.os.Bundle;
6
+ import android.preference.PreferenceManager;
7
+ import android.util.Log;
8
+ import android.os.Handler;
9
+ import android.os.Looper;
10
+
11
+ import com.google.firebase.messaging.FirebaseMessagingService;
12
+ import com.google.firebase.messaging.RemoteMessage;
13
+ import com.vibes.vibes.BuildConfig;
14
+ import com.vibes.vibes.PushPayloadParser;
15
+ import com.vibes.vibes.Vibes;
16
+
17
+ import com.facebook.react.ReactApplication;
18
+ import com.facebook.react.ReactInstanceManager;
19
+ import com.facebook.react.bridge.ReactContext;
20
+
21
+ import java.util.Map;
22
+ import com.vibes.push.rn.plugin.VibesModule;
23
+ import com.vibes.push.rn.plugin.PushEvtEmitter;
24
+ import static com.vibes.push.rn.plugin.VibesModule.TAG;
25
+ import static com.vibes.push.rn.plugin.VibesModule.TOKEN_KEY;
26
+ import static com.vibes.push.rn.plugin.VibesModule.DEVICE_REGISTERED;
27
+
28
+ public class Fms extends FirebaseMessagingService {
29
+
30
+ @Override
31
+ public void onMessageReceived(RemoteMessage message){
32
+
33
+ Log.d(TAG, "Push message received. Processing");
34
+ PushPayloadParser pushModel = this.createPushPayloadParser(message.getData());
35
+ if (BuildConfig.DEBUG) {
36
+ PayloadWrapper wrapper = new PayloadWrapper(pushModel);
37
+ Log.d(TAG, wrapper.toString());
38
+ }
39
+ // pass the received payload to the handleNotification SDK method. It takes care
40
+ // of displaying the message
41
+ try {
42
+ Vibes.getInstance().handleNotification(getApplicationContext(), message.getData());
43
+ if (!pushModel.isSilentPush()) {
44
+ emitPayload(pushModel);
45
+ }
46
+ } catch(IllegalStateException e) {
47
+ Handler handler = new Handler(Looper.getMainLooper());
48
+ handler.post(new Runnable() {
49
+ public void run() {
50
+ Vibes.getInstance().handleNotification(getApplicationContext(), message.getData());
51
+ if (!pushModel.isSilentPush()) {
52
+ emitPayload(pushModel);
53
+ }
54
+ }
55
+ });
56
+ }
57
+ }
58
+ /**
59
+ * This is invoked everytime the application generates a new Firebase push
60
+ * token, which is then sent to the Vibes server to be able to target this
61
+ * device with push messages.
62
+ *
63
+ * @param pushToken
64
+ */
65
+ @Override
66
+ public void onNewToken(String pushToken) {
67
+ super.onNewToken(pushToken);
68
+ Log.d(TAG, "Firebase token obtained from Fms as " + pushToken);
69
+ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
70
+ SharedPreferences.Editor editor = preferences.edit();
71
+ editor.putString(TOKEN_KEY, pushToken);
72
+ editor.apply();
73
+
74
+ boolean registered = preferences.getBoolean(DEVICE_REGISTERED, false);
75
+ if (registered) {
76
+ VibesModule.registerPush(pushToken);
77
+ }
78
+ }
79
+
80
+ public PushPayloadParser createPushPayloadParser(Map<String, String> map) {
81
+ return new PushPayloadParser(map);
82
+ }
83
+
84
+ private void emitPayload(PushPayloadParser pushModel) {
85
+ final FirebaseMessagingService serviceRef = this;
86
+ // We need to run this on the main thread, as the React code assumes that is true.
87
+ // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
88
+ // "Can't create handler inside thread that has not called Looper.prepare()"
89
+ Handler handler = new Handler(Looper.getMainLooper());
90
+ handler.post(new Runnable() {
91
+ public void run() {
92
+ // Construct and load our normal React JS code bundle
93
+ final ReactInstanceManager mReactInstanceManager = ((ReactApplication) serviceRef.getApplication()).getReactNativeHost().getReactInstanceManager();
94
+ ReactContext context = mReactInstanceManager.getCurrentReactContext();
95
+ // If it's constructed, send a notification
96
+ if (context != null) {
97
+ PushEvtEmitter pushEmitter = new PushEvtEmitter(context);
98
+ pushEmitter.notifyPushReceived(pushModel);
99
+ } else {
100
+ // Otherwise wait for construction, then send the notification
101
+ mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {
102
+ public void onReactContextInitialized(ReactContext context) {
103
+ PushEvtEmitter pushEmitter = new PushEvtEmitter(context);
104
+ pushEmitter.notifyPushReceived(pushModel);
105
+ mReactInstanceManager.removeReactInstanceEventListener(this);
106
+ }
107
+ });
108
+ if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
109
+ // Construct it in the background
110
+ mReactInstanceManager.createReactContextInBackground();
111
+ }
112
+ }
113
+ }
114
+ });
115
+ }
116
+ }
@@ -0,0 +1,23 @@
1
+
2
+ package com.vibes.push.rn.plugin.notifications;
3
+
4
+ import com.vibes.vibes.PushPayloadParser;
5
+
6
+ public class PayloadWrapper {
7
+
8
+ private final PushPayloadParser payload;
9
+
10
+
11
+ public PayloadWrapper(PushPayloadParser payload) {
12
+ this.payload = payload;
13
+ }
14
+
15
+ @Override
16
+ public String toString() {
17
+ return "PayloadWrapper{" +
18
+ "title=" + payload.getTitle() +", body=" + payload.getBody()+", channel=" + payload.getChannel()+", notification_channel=" + payload.getNotificationChannel()
19
+ +", silent_push=" + payload.isSilentPush()+", sound=" + payload.getSound()+", rich_media_url=" + payload.getRichPushMediaURL()+", vibes_collapse_id=" + payload.getVibesCollapseId()
20
+ +", priority=" + payload.getPriority()+", custom_client_data=" + payload.getCustomClientData()+ '}';
21
+ }
22
+
23
+ }