From 9cc07ee2625f6715c1a96dce442ad55f1d698aea Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Mon, 31 Jul 2017 10:18:43 -0400 Subject: [PATCH] Update MessagingService.java --- .../evollu/react/fcm/MessagingService.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/evollu/react/fcm/MessagingService.java b/android/src/main/java/com/evollu/react/fcm/MessagingService.java index c85deff..fe48fcf 100644 --- a/android/src/main/java/com/evollu/react/fcm/MessagingService.java +++ b/android/src/main/java/com/evollu/react/fcm/MessagingService.java @@ -22,7 +22,33 @@ public class MessagingService extends FirebaseMessagingService { i.putExtra("data", remoteMessage); handleBadge(remoteMessage); buildLocalNotification(remoteMessage); - sendOrderedBroadcast(i, null); + + // We need to run this on the main thread, as the React code assumes that is true. + // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers: + // "Can't create handler inside thread that has not called Looper.prepare()" + 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) { + context.sendOrderedBroadcast(message, null); + } else { + // Otherwise wait for construction, then send the notification + mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { + public void onReactContextInitialized(ReactContext context) { + context.sendOrderedBroadcast(message, null); + } + }); + if (!mReactInstanceManager.hasStartedCreatingInitialContext()) { + // Construct it in the background + mReactInstanceManager.createReactContextInBackground(); + } + } + } + }); } public void handleBadge(RemoteMessage remoteMessage) { @@ -61,4 +87,4 @@ public class MessagingService extends FirebaseMessagingService { } } -} \ No newline at end of file +} -- 2.26.2