diff --git a/android/src/main/java/com/evollu/react/fcm/InstanceIdService.java b/android/src/main/java/com/evollu/react/fcm/InstanceIdService.java index 9ac9ef540dd7525158058ebdd1032cbaf6c65f7f..f01c160ceab4702d480c76a5290ab9f527b5224c 100644 --- a/android/src/main/java/com/evollu/react/fcm/InstanceIdService.java +++ b/android/src/main/java/com/evollu/react/fcm/InstanceIdService.java @@ -2,9 +2,14 @@ package com.evollu.react.fcm; import android.content.Intent; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; +import com.facebook.react.ReactApplication; +import com.facebook.react.ReactInstanceManager; +import com.facebook.react.bridge.ReactContext; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService; @@ -25,11 +30,35 @@ public class InstanceIdService extends FirebaseInstanceIdService { Log.d(TAG, "Refreshed token: " + refreshedToken); // Broadcast refreshed token - Intent i = new Intent("com.evollu.react.fcm.FCMRefreshToken"); Bundle bundle = new Bundle(); bundle.putString("token", refreshedToken); i.putExtras(bundle); - LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i); + + final Intent message = i; + + Handler handler = new Handler(Looper.getMainLooper()); + handler.post(new Runnable() { + public void run() { + // Construct and load our normal React JS code bundle + ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); + ReactContext context = mReactInstanceManager.getCurrentReactContext(); + // If it's constructed, send a notification + if (context != null) { + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message); + } else { + // Otherwise wait for construction, then send the notification + mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { + public void onReactContextInitialized(ReactContext context) { + LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(message); + } + }); + if (!mReactInstanceManager.hasStartedCreatingInitialContext()) { + // Construct it in the background + mReactInstanceManager.createReactContextInBackground(); + } + } + } + }); } }