Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Handle localization for notification title and body #1837

Merged
merged 2 commits into from
Jan 24, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}