|
| 1 | +package com.b8ne.RNPusherPushNotifications; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | + |
| 5 | +import android.os.Handler; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.os.Looper; |
| 8 | +import android.content.Intent; |
| 9 | + |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +import com.facebook.react.ReactApplication; |
| 13 | +import com.facebook.react.ReactInstanceManager; |
| 14 | +import com.facebook.react.bridge.Arguments; |
| 15 | +import com.facebook.react.bridge.ReactContext; |
| 16 | +import com.facebook.react.bridge.WritableMap; |
| 17 | +import com.facebook.react.bridge.WritableNativeMap; |
| 18 | +import com.facebook.react.modules.core.DeviceEventManagerModule; |
| 19 | + |
| 20 | +public class NotificationsMessagingService { |
| 21 | + |
| 22 | + private static String notificationEvent = "notification"; |
| 23 | + private static ReactContext context; |
| 24 | + private static ReactInstanceManager reactInstanceManager; |
| 25 | + |
| 26 | + public static void read(final ReactInstanceManager reactInstanceManager, Activity reactActivity) { |
| 27 | + Intent intent = reactActivity.getIntent(); |
| 28 | + final WritableMap map = new WritableNativeMap(); |
| 29 | + |
| 30 | + boolean launchedFromHistory = intent != null ? (intent.getFlags() & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0 : false; |
| 31 | + |
| 32 | + Bundle extras = intent.getExtras(); |
| 33 | + if (!launchedFromHistory & extras != null) { |
| 34 | + WritableMap payload = Arguments.createMap(); |
| 35 | + |
| 36 | + for (String key : extras.keySet()) { |
| 37 | + Object value = extras.get(key); |
| 38 | + payload.putString(key, value.toString()); |
| 39 | + } |
| 40 | + map.putMap("data", payload); |
| 41 | + |
| 42 | + if (payload != null) { |
| 43 | + // We need to run this on the main thread, as the React code assumes that is true. |
| 44 | + // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers: |
| 45 | + // "Can't create handler inside thread that has not called Looper.prepare()" |
| 46 | + Handler handler = new Handler(Looper.getMainLooper()); |
| 47 | + handler.post(new Runnable() { |
| 48 | + public void run() { |
| 49 | + // Construct and load our normal React JS code data |
| 50 | + // ReactInstanceManager reactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); |
| 51 | + ReactContext context = reactInstanceManager.getCurrentReactContext(); |
| 52 | + // If it's constructed, send a notification |
| 53 | + if (context != null) { |
| 54 | + context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 55 | + .emit(notificationEvent, map); |
| 56 | + } else { |
| 57 | + // Otherwise wait for construction, then send the notification |
| 58 | + reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { |
| 59 | + public void onReactContextInitialized(ReactContext context) { |
| 60 | + context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) |
| 61 | + .emit(notificationEvent, map); |
| 62 | + } |
| 63 | + }); |
| 64 | + if (!reactInstanceManager.hasStartedCreatingInitialContext()) { |
| 65 | + // Construct it in the background |
| 66 | + reactInstanceManager.createReactContextInBackground(); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments