Search in sources :

Example 21 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext 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

ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)21 ReactMethod (com.facebook.react.bridge.ReactMethod)10 ReactInstanceManager (com.facebook.react.ReactInstanceManager)5 Intent (android.content.Intent)4 Handler (android.os.Handler)4 InterstitialAd (com.facebook.ads.InterstitialAd)4 ReactContext (com.facebook.react.bridge.ReactContext)4 NativeViewHierarchyManager (com.facebook.react.uimanager.NativeViewHierarchyManager)4 UIBlock (com.facebook.react.uimanager.UIBlock)4 UIManagerModule (com.facebook.react.uimanager.UIManagerModule)4 ReactApplication (com.facebook.react.ReactApplication)3 WritableMap (com.facebook.react.bridge.WritableMap)3 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 IntentFilter (android.content.IntentFilter)2 NativeAdsManager (com.facebook.ads.NativeAdsManager)2 RCTNativeAppEventEmitter (com.facebook.react.modules.core.RCTNativeAppEventEmitter)2 File (java.io.File)2 Activity (android.app.Activity)1 Bitmap (android.graphics.Bitmap)1