|
3 | 3 | import com.google.firebase.messaging.FirebaseMessagingService; |
4 | 4 | import com.google.firebase.messaging.RemoteMessage; |
5 | 5 |
|
| 6 | +import android.os.Handler; |
| 7 | +import android.os.Looper; |
| 8 | +import android.util.Log; |
| 9 | +import android.support.annotation.NonNull; |
| 10 | + |
6 | 11 | import com.dieam.reactnativepushnotification.modules.RNReceivedMessageHandler; |
| 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.ReactApplicationContext; |
| 16 | +import com.facebook.react.bridge.ReactContext; |
| 17 | +import com.facebook.react.bridge.WritableMap; |
| 18 | + |
| 19 | +import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG; |
7 | 20 |
|
8 | 21 | public class RNPushNotificationListenerService extends FirebaseMessagingService { |
9 | 22 |
|
10 | 23 | private RNReceivedMessageHandler mMessageReceivedHandler = new RNReceivedMessageHandler(this); |
11 | 24 |
|
12 | 25 | @Override |
13 | 26 | public void onNewToken(String token) { |
14 | | - mMessageReceivedHandler.onNewToken(token); |
| 27 | + final String deviceToken = token; |
| 28 | + Log.d(LOG_TAG, "Refreshed token: " + deviceToken); |
| 29 | + |
| 30 | + Handler handler = new Handler(Looper.getMainLooper()); |
| 31 | + handler.post(new Runnable() { |
| 32 | + public void run() { |
| 33 | + // Construct and load our normal React JS code bundle |
| 34 | + ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager(); |
| 35 | + ReactContext context = mReactInstanceManager.getCurrentReactContext(); |
| 36 | + // If it's constructed, send a notification |
| 37 | + if (context != null) { |
| 38 | + handleNewToken((ReactApplicationContext) context, deviceToken); |
| 39 | + } else { |
| 40 | + // Otherwise wait for construction, then send the notification |
| 41 | + mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { |
| 42 | + public void onReactContextInitialized(ReactContext context) { |
| 43 | + handleNewToken((ReactApplicationContext) context, deviceToken); |
| 44 | + } |
| 45 | + }); |
| 46 | + if (!mReactInstanceManager.hasStartedCreatingInitialContext()) { |
| 47 | + // Construct it in the background |
| 48 | + mReactInstanceManager.createReactContextInBackground(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + private void handleNewToken(ReactApplicationContext context, String token) { |
| 56 | + RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context); |
| 57 | + |
| 58 | + WritableMap params = Arguments.createMap(); |
| 59 | + params.putString("deviceToken", token); |
| 60 | + jsDelivery.sendEvent("remoteNotificationsRegistered", params); |
15 | 61 | } |
16 | 62 |
|
17 | 63 | @Override |
|
0 commit comments