Search in sources :

Example 1 with FirebaseMessagingService

use of com.google.firebase.messaging.FirebaseMessagingService in project react-native-push-notification by zo0r.

the class RNPushNotificationListenerService method onNewToken.

@Override
public void onNewToken(String token) {
    final String deviceToken = token;
    final FirebaseMessagingService serviceRef = (this.mFirebaseServiceDelegate == null) ? this : this.mFirebaseServiceDelegate;
    Log.d(LOG_TAG, "Refreshed token: " + deviceToken);
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {

        public void run() {
            // Construct and load our normal React JS code bundle
            final ReactInstanceManager mReactInstanceManager = ((ReactApplication) serviceRef.getApplication()).getReactNativeHost().getReactInstanceManager();
            ReactContext context = mReactInstanceManager.getCurrentReactContext();
            // If it's constructed, send a notification
            if (context != null) {
                handleNewToken((ReactApplicationContext) context, deviceToken);
            } else {
                // Otherwise wait for construction, then send the notification
                mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                    public void onReactContextInitialized(ReactContext context) {
                        handleNewToken((ReactApplicationContext) context, deviceToken);
                        mReactInstanceManager.removeReactInstanceEventListener(this);
                    }
                });
                if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                    // Construct it in the background
                    mReactInstanceManager.createReactContextInBackground();
                }
            }
        }
    });
}
Also used : FirebaseMessagingService(com.google.firebase.messaging.FirebaseMessagingService) ReactInstanceManager(com.facebook.react.ReactInstanceManager) ReactContext(com.facebook.react.bridge.ReactContext) Handler(android.os.Handler) ReactApplication(com.facebook.react.ReactApplication) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext)

Aggregations

Handler (android.os.Handler)1 ReactApplication (com.facebook.react.ReactApplication)1 ReactInstanceManager (com.facebook.react.ReactInstanceManager)1 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)1 ReactContext (com.facebook.react.bridge.ReactContext)1 FirebaseMessagingService (com.google.firebase.messaging.FirebaseMessagingService)1