Commit 9cc07ee2 authored by Libin Lu's avatar Libin Lu Committed by GitHub

Update MessagingService.java

parent edbf2ac3
......@@ -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
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment