Skip to content

Commit

Permalink
Merge pull request #1837 from plus-/master
Browse files Browse the repository at this point in the history
Handle localization for notification title and body
  • Loading branch information
Boris Tacyniak authored Jan 24, 2021
2 parents 4fa8ac1 + 8161e1b commit a4c6459
Showing 1 changed file with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.content.Context;
import android.util.Log;
import android.net.Uri;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -46,8 +47,11 @@ public void handleReceivedMessage(RemoteMessage message) {
// ^ It's null when message is from GCM
RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());

bundle.putString("title", remoteNotification.getTitle());
bundle.putString("message", remoteNotification.getBody());
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());
Expand Down Expand Up @@ -178,4 +182,28 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
pushNotificationHelper.sendToNotificationCentre(bundle);
}
}

private String getLocalizedString(String text, String locKey, String[] locArgs) {
if(text != null) {
return text;
}

Context context = mFirebaseMessagingService.getApplicationContext();
String packageName = context.getPackageName();

String result = null;

if (locKey != null) {
int id = context.getResources().getIdentifier(locKey, "string", packageName);
if (id != 0) {
if (locArgs != null) {
result = context.getResources().getString(id, (Object[]) locArgs);
} else {
result = context.getResources().getString(id);
}
}
}

return result;
}
}

0 comments on commit a4c6459

Please sign in to comment.