Search in sources :

Example 1 with ReactApplication

use of com.facebook.react.ReactApplication in project react-native-push-notification by zo0r.

the class RNPushNotificationListenerService method onMessageReceived.

@Override
public void onMessageReceived(String from, final Bundle bundle) {
    JSONObject data = getPushData(bundle.getString("data"));
    if (data != null) {
        if (!bundle.containsKey("message")) {
            bundle.putString("message", data.optString("alert", "Notification received"));
        }
        if (!bundle.containsKey("title")) {
            bundle.putString("title", data.optString("title", null));
        }
        if (!bundle.containsKey("sound")) {
            bundle.putString("soundName", data.optString("sound", null));
        }
        if (!bundle.containsKey("color")) {
            bundle.putString("color", data.optString("color", null));
        }
        final int badge = data.optInt("badge", -1);
        if (badge >= 0) {
            ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(this, badge);
        }
    }
    Log.v(LOG_TAG, "onMessageReceived: " + bundle);
    // 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) {
                handleRemotePushNotification((ReactApplicationContext) context, bundle);
            } else {
                // Otherwise wait for construction, then send the notification
                mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                    public void onReactContextInitialized(ReactContext context) {
                        handleRemotePushNotification((ReactApplicationContext) context, bundle);
                    }
                });
                if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                    // Construct it in the background
                    mReactInstanceManager.createReactContextInBackground();
                }
            }
        }
    });
}
Also used : JSONObject(org.json.JSONObject) 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)

Example 2 with ReactApplication

use of com.facebook.react.ReactApplication in project react-native-fcm by evollu.

the class InstanceIdService method onTokenRefresh.

/**
 * Called if InstanceID token is updated. This may occur if the security of
 * the previous token had been compromised. This call is initiated by the
 * InstanceID provider.
 */
// [START refresh_token]
@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    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);
    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();
                }
            }
        }
    });
}
Also used : ReactInstanceManager(com.facebook.react.ReactInstanceManager) ReactContext(com.facebook.react.bridge.ReactContext) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) ReactApplication(com.facebook.react.ReactApplication)

Example 3 with ReactApplication

use of com.facebook.react.ReactApplication in project react-native-fcm by evollu.

the class MessagingService method onMessageReceived.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d(TAG, "Remote message received");
    Intent i = new Intent("com.evollu.react.fcm.ReceiveNotification");
    i.putExtra("data", remoteMessage);
    handleBadge(remoteMessage);
    buildLocalNotification(remoteMessage);
    final Intent message = i;
    // 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) {
                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();
                }
            }
        }
    });
}
Also used : ReactInstanceManager(com.facebook.react.ReactInstanceManager) ReactContext(com.facebook.react.bridge.ReactContext) Handler(android.os.Handler) Intent(android.content.Intent) ReactApplication(com.facebook.react.ReactApplication)

Aggregations

Handler (android.os.Handler)3 ReactApplication (com.facebook.react.ReactApplication)3 ReactInstanceManager (com.facebook.react.ReactInstanceManager)3 ReactContext (com.facebook.react.bridge.ReactContext)3 Intent (android.content.Intent)2 Bundle (android.os.Bundle)1 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)1 JSONObject (org.json.JSONObject)1