From 109f8309cfcf708b21c964e797cdb1635478a090 Mon Sep 17 00:00:00 2001 From: Vojtech Novak Date: Tue, 24 Jul 2018 15:17:27 +0200 Subject: [PATCH] use localized title and body unless normal title and body are sent remove unused import --- .../java/io/invertase/firebase/Utils.java | 4 ++ .../RNFirebaseNotifications.java | 41 +++++++++++++++++-- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/io/invertase/firebase/Utils.java b/android/src/main/java/io/invertase/firebase/Utils.java index 6e84a96f..293f0f7c 100644 --- a/android/src/main/java/io/invertase/firebase/Utils.java +++ b/android/src/main/java/io/invertase/firebase/Utils.java @@ -156,4 +156,8 @@ public class Utils { return false; } + + public static int getResId(Context ctx, String resName) { + return ctx.getResources().getIdentifier(resName, "string", ctx.getPackageName()); + } } diff --git a/android/src/main/java/io/invertase/firebase/notifications/RNFirebaseNotifications.java b/android/src/main/java/io/invertase/firebase/notifications/RNFirebaseNotifications.java index 3e9704ce..cd0cdca1 100644 --- a/android/src/main/java/io/invertase/firebase/notifications/RNFirebaseNotifications.java +++ b/android/src/main/java/io/invertase/firebase/notifications/RNFirebaseNotifications.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v4.app.RemoteInput; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; @@ -30,6 +31,8 @@ import io.invertase.firebase.Utils; import io.invertase.firebase.messaging.RNFirebaseMessagingService; import me.leolin.shortcutbadger.ShortcutBadger; +import static io.invertase.firebase.Utils.getResId; + public class RNFirebaseNotifications extends ReactContextBaseJavaModule implements ActivityEventListener { private static final String BADGE_FILE = "BadgeCountFile"; private static final String BADGE_KEY = "BadgeCount"; @@ -269,7 +272,10 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen WritableMap dataMap = Arguments.createMap(); // Cross platform notification properties - notificationMap.putString("body", notification.getBody()); + String body = getNotificationBody(notification); + if (body != null) { + notificationMap.putString("body", body); + } if (message.getData() != null) { for (Map.Entry e : message.getData().entrySet()) { dataMap.putString(e.getKey(), e.getValue()); @@ -282,8 +288,9 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen if (notification.getSound() != null) { notificationMap.putString("sound", notification.getSound()); } - if (notification.getTitle() != null) { - notificationMap.putString("title", notification.getTitle()); + String title = getNotificationTitle(notification); + if (title != null) { + notificationMap.putString("title", title); } // Android specific notification properties @@ -308,6 +315,34 @@ public class RNFirebaseNotifications extends ReactContextBaseJavaModule implemen return notificationMap; } + private @Nullable String getNotificationTitle(RemoteMessage.Notification notification) { + String title, titleLocKey; + if ((title = notification.getTitle()) != null) { + return title; + } else if ((titleLocKey = notification.getTitleLocalizationKey()) != null) { + String[] titleLocArgs = notification.getTitleLocalizationArgs(); + Context ctx = getReactApplicationContext(); + int resId = getResId(ctx, titleLocKey); + return ctx.getResources().getString(resId, titleLocArgs); + } else { + return null; + } + } + + private @Nullable String getNotificationBody(RemoteMessage.Notification notification) { + String body = notification.getBody(), bodyLocKey = notification.getBodyLocalizationKey(); + if (body != null) { + return body; + } else if (bodyLocKey != null) { + String[] bodyLocArgs = notification.getBodyLocalizationArgs(); + Context ctx = getReactApplicationContext(); + int resId = getResId(ctx, bodyLocKey); + return ctx.getResources().getString(resId, bodyLocArgs); + } else { + return null; + } + } + private class RemoteNotificationReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {