Search in sources :

Example 16 with ReactInstanceManager

use of com.facebook.react.ReactInstanceManager in project react-native-fbads by callstack.

the class ReactNativeFlipper method initializeFlipper.

public static void initializeFlipper(Context context, ReactInstanceManager reactInstanceManager) {
    if (FlipperUtils.shouldEnableFlipper(context)) {
        final FlipperClient client = AndroidFlipperClient.getInstance(context);
        client.addPlugin(new InspectorFlipperPlugin(context, DescriptorMapping.withDefaults()));
        client.addPlugin(new ReactFlipperPlugin());
        client.addPlugin(new DatabasesFlipperPlugin(context));
        client.addPlugin(new SharedPreferencesFlipperPlugin(context));
        client.addPlugin(CrashReporterPlugin.getInstance());
        NetworkFlipperPlugin networkFlipperPlugin = new NetworkFlipperPlugin();
        NetworkingModule.setCustomClientBuilder(new NetworkingModule.CustomClientBuilder() {

            @Override
            public void apply(OkHttpClient.Builder builder) {
                builder.addNetworkInterceptor(new FlipperOkhttpInterceptor(networkFlipperPlugin));
            }
        });
        client.addPlugin(networkFlipperPlugin);
        client.start();
        // Fresco Plugin needs to ensure that ImagePipelineFactory is initialized
        // Hence we run if after all native modules have been initialized
        ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
        if (reactContext == null) {
            reactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                @Override
                public void onReactContextInitialized(ReactContext reactContext) {
                    reactInstanceManager.removeReactInstanceEventListener(this);
                    reactContext.runOnNativeModulesQueueThread(new Runnable() {

                        @Override
                        public void run() {
                            client.addPlugin(new FrescoFlipperPlugin());
                        }
                    });
                }
            });
        } else {
            client.addPlugin(new FrescoFlipperPlugin());
        }
    }
}
Also used : FlipperOkhttpInterceptor(com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor) AndroidFlipperClient(com.facebook.flipper.android.AndroidFlipperClient) FlipperClient(com.facebook.flipper.core.FlipperClient) OkHttpClient(okhttp3.OkHttpClient) ReactInstanceManager(com.facebook.react.ReactInstanceManager) InspectorFlipperPlugin(com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin) ReactFlipperPlugin(com.facebook.flipper.plugins.react.ReactFlipperPlugin) DatabasesFlipperPlugin(com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin) ReactContext(com.facebook.react.bridge.ReactContext) FrescoFlipperPlugin(com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin) NetworkFlipperPlugin(com.facebook.flipper.plugins.network.NetworkFlipperPlugin) SharedPreferencesFlipperPlugin(com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin) NetworkingModule(com.facebook.react.modules.network.NetworkingModule)

Example 17 with ReactInstanceManager

use of com.facebook.react.ReactInstanceManager in project react-native-twilio-programmable-voice by hoxfon.

the class VoiceFirebaseMessagingService method onMessageReceived.

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Bundle data: " + remoteMessage.getData());
    }
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Map<String, String> data = remoteMessage.getData();
        // If notification ID is not provided by the user for push notification, generate one at random
        Random randomNumberGenerator = new Random(System.currentTimeMillis());
        final int notificationId = randomNumberGenerator.nextInt();
        Voice.handleMessage(this, data, new MessageListener() {

            @Override
            public void onCallInvite(final CallInvite callInvite) {
                // 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) {
                            int appImportance = callNotificationManager.getApplicationImportance((ReactApplicationContext) context);
                            if (BuildConfig.DEBUG) {
                                Log.d(TAG, "CONTEXT present appImportance = " + appImportance);
                            }
                            Intent launchIntent = callNotificationManager.getLaunchIntent((ReactApplicationContext) context, notificationId, callInvite, false, appImportance);
                            // app is not in foreground
                            if (appImportance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                                context.startActivity(launchIntent);
                            }
                            VoiceFirebaseMessagingService.this.handleIncomingCall((ReactApplicationContext) context, notificationId, callInvite, launchIntent);
                        } else {
                            // Otherwise wait for construction, then handle the incoming call
                            mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                                public void onReactContextInitialized(ReactContext context) {
                                    int appImportance = callNotificationManager.getApplicationImportance((ReactApplicationContext) context);
                                    if (BuildConfig.DEBUG) {
                                        Log.d(TAG, "CONTEXT not present appImportance = " + appImportance);
                                    }
                                    Intent launchIntent = callNotificationManager.getLaunchIntent((ReactApplicationContext) context, notificationId, callInvite, true, appImportance);
                                    context.startActivity(launchIntent);
                                    VoiceFirebaseMessagingService.this.handleIncomingCall((ReactApplicationContext) context, notificationId, callInvite, launchIntent);
                                }
                            });
                            if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                                // Construct it in the background
                                mReactInstanceManager.createReactContextInBackground();
                            }
                        }
                    }
                });
            }

            @Override
            public void onError(MessageException messageException) {
                Log.e(TAG, "Error handling FCM message" + messageException.toString());
            }
        });
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}
Also used : ReactInstanceManager(com.facebook.react.ReactInstanceManager) MessageListener(com.twilio.voice.MessageListener) Handler(android.os.Handler) Intent(android.content.Intent) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) Random(java.util.Random) ReactContext(com.facebook.react.bridge.ReactContext) MessageException(com.twilio.voice.MessageException) CallInvite(com.twilio.voice.CallInvite)

Example 18 with ReactInstanceManager

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

the class RNPushNotificationActions method onReceive.

@Override
public void onReceive(final Context context, Intent intent) {
    String intentActionPrefix = context.getPackageName() + ".ACTION_";
    Log.i(LOG_TAG, "RNPushNotificationBootEventReceiver loading scheduled notifications");
    if (null == intent.getAction() || !intent.getAction().startsWith(intentActionPrefix)) {
        return;
    }
    final Bundle bundle = intent.getBundleExtra("notification");
    Bundle remoteInput = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT_WATCH) {
        remoteInput = RemoteInput.getResultsFromIntent(intent);
    }
    if (remoteInput != null) {
        // Add to reply_text the text written by the user in the notification
        bundle.putCharSequence("reply_text", remoteInput.getCharSequence(KEY_TEXT_REPLY));
    }
    // Dismiss the notification popup.
    NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    int notificationID = Integer.parseInt(bundle.getString("id"));
    boolean autoCancel = bundle.getBoolean("autoCancel", true);
    if (autoCancel) {
        if (bundle.containsKey("tag")) {
            String tag = bundle.getString("tag");
            manager.cancel(tag, notificationID);
        } else {
            manager.cancel(notificationID);
        }
    }
    boolean invokeApp = bundle.getBoolean("invokeApp", true);
    // Notify the action.
    if (invokeApp) {
        RNPushNotificationHelper helper = new RNPushNotificationHelper((Application) context.getApplicationContext());
        helper.invokeApp(bundle);
        context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
    } else {
        // 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
                final ReactInstanceManager mReactInstanceManager = ((ReactApplication) context.getApplicationContext()).getReactNativeHost().getReactInstanceManager();
                ReactContext context = mReactInstanceManager.getCurrentReactContext();
                // If it's constructed, send a notification
                if (context != null) {
                    RNPushNotificationJsDelivery mJsDelivery = new RNPushNotificationJsDelivery(context);
                    mJsDelivery.notifyNotificationAction(bundle);
                } else {
                    // Otherwise wait for construction, then send the notification
                    mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                        public void onReactContextInitialized(ReactContext context) {
                            RNPushNotificationJsDelivery mJsDelivery = new RNPushNotificationJsDelivery(context);
                            mJsDelivery.notifyNotificationAction(bundle);
                            mReactInstanceManager.removeReactInstanceEventListener(this);
                        }
                    });
                    if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                        // Construct it in the background
                        mReactInstanceManager.createReactContextInBackground();
                    }
                }
            }
        });
    }
}
Also used : NotificationManager(android.app.NotificationManager) ReactInstanceManager(com.facebook.react.ReactInstanceManager) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) ReactContext(com.facebook.react.bridge.ReactContext) ReactApplication(com.facebook.react.ReactApplication)

Example 19 with ReactInstanceManager

use of com.facebook.react.ReactInstanceManager 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)

Example 20 with ReactInstanceManager

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

the class RNReceivedMessageHandler method handleReceivedMessage.

public void handleReceivedMessage(RemoteMessage message) {
    String from = message.getFrom();
    RemoteMessage.Notification remoteNotification = message.getNotification();
    final Bundle bundle = new Bundle();
    // data has it
    if (remoteNotification != null) {
        // ^ It's null when message is from GCM
        RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());
        String title = getLocalizedString(remoteNotification.getTitle(), remoteNotification.getTitleLocalizationKey(), remoteNotification.getTitleLocalizationArgs());
        String body = getLocalizedString(remoteNotification.getBody(), remoteNotification.getBodyLocalizationKey(), remoteNotification.getBodyLocalizationArgs());
        bundle.putString("title", title);
        bundle.putString("message", body);
        bundle.putString("sound", remoteNotification.getSound());
        bundle.putString("color", remoteNotification.getColor());
        bundle.putString("tag", remoteNotification.getTag());
        if (remoteNotification.getIcon() != null) {
            bundle.putString("smallIcon", remoteNotification.getIcon());
        } else {
            bundle.putString("smallIcon", "ic_notification");
        }
        if (remoteNotification.getChannelId() != null) {
            bundle.putString("channelId", remoteNotification.getChannelId());
        } else {
            bundle.putString("channelId", config.getNotificationDefaultChannelId());
        }
        Integer visibilty = remoteNotification.getVisibility();
        String visibilityString = "private";
        if (visibilty != null) {
            switch(visibilty) {
                case NotificationCompat.VISIBILITY_PUBLIC:
                    visibilityString = "public";
                    break;
                case NotificationCompat.VISIBILITY_SECRET:
                    visibilityString = "secret";
                    break;
            }
        }
        bundle.putString("visibility", visibilityString);
        Integer priority = remoteNotification.getNotificationPriority();
        String priorityString = "high";
        if (priority != null) {
            switch(priority) {
                case NotificationCompat.PRIORITY_MAX:
                    priorityString = "max";
                    break;
                case NotificationCompat.PRIORITY_LOW:
                    priorityString = "low";
                    break;
                case NotificationCompat.PRIORITY_MIN:
                    priorityString = "min";
                    break;
                case NotificationCompat.PRIORITY_DEFAULT:
                    priorityString = "default";
                    break;
            }
        }
        bundle.putString("priority", priorityString);
        Uri uri = remoteNotification.getImageUrl();
        if (uri != null) {
            String imageUrl = uri.toString();
            bundle.putString("bigPictureUrl", imageUrl);
            bundle.putString("largeIconUrl", imageUrl);
        }
    }
    Bundle dataBundle = new Bundle();
    Map<String, String> notificationData = message.getData();
    for (Map.Entry<String, String> entry : notificationData.entrySet()) {
        dataBundle.putString(entry.getKey(), entry.getValue());
    }
    bundle.putParcelable("data", dataBundle);
    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
            final ReactInstanceManager mReactInstanceManager = ((ReactApplication) mFirebaseMessagingService.getApplication()).getReactNativeHost().getReactInstanceManager();
            ReactContext context = mReactInstanceManager.getCurrentReactContext();
            // If it's constructed, send a notificationre
            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);
                        mReactInstanceManager.removeReactInstanceEventListener(this);
                    }
                });
                if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                    // Construct it in the background
                    mReactInstanceManager.createReactContextInBackground();
                }
            }
        }
    });
}
Also used : RemoteMessage(com.google.firebase.messaging.RemoteMessage) ReactInstanceManager(com.facebook.react.ReactInstanceManager) Bundle(android.os.Bundle) Handler(android.os.Handler) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) Uri(android.net.Uri) ReactContext(com.facebook.react.bridge.ReactContext) ReactApplication(com.facebook.react.ReactApplication) Map(java.util.Map)

Aggregations

ReactInstanceManager (com.facebook.react.ReactInstanceManager)23 ReactContext (com.facebook.react.bridge.ReactContext)19 AndroidFlipperClient (com.facebook.flipper.android.AndroidFlipperClient)11 FlipperClient (com.facebook.flipper.core.FlipperClient)11 DatabasesFlipperPlugin (com.facebook.flipper.plugins.databases.DatabasesFlipperPlugin)11 FrescoFlipperPlugin (com.facebook.flipper.plugins.fresco.FrescoFlipperPlugin)11 InspectorFlipperPlugin (com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin)11 FlipperOkhttpInterceptor (com.facebook.flipper.plugins.network.FlipperOkhttpInterceptor)11 NetworkFlipperPlugin (com.facebook.flipper.plugins.network.NetworkFlipperPlugin)11 ReactFlipperPlugin (com.facebook.flipper.plugins.react.ReactFlipperPlugin)11 SharedPreferencesFlipperPlugin (com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin)11 NetworkingModule (com.facebook.react.modules.network.NetworkingModule)11 OkHttpClient (okhttp3.OkHttpClient)11 Handler (android.os.Handler)7 ReactApplication (com.facebook.react.ReactApplication)6 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)5 Intent (android.content.Intent)4 Bundle (android.os.Bundle)3 EventEmitter (com.reactnativenavigation.react.events.EventEmitter)2 ChildControllersRegistry (com.reactnativenavigation.viewcontrollers.child.ChildControllersRegistry)2