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

Commit 1101e84

Browse files
committed
Handle localization for notification title and body
1 parent 4fa8ac1 commit 1101e84

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNReceivedMessageHandler.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ public void handleReceivedMessage(RemoteMessage message) {
4646
// ^ It's null when message is from GCM
4747
RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());
4848

49-
bundle.putString("title", remoteNotification.getTitle());
50-
bundle.putString("message", remoteNotification.getBody());
49+
bundle.putString("title", getLocalizedString(remoteNotification.getTitleLocalizationKey(),
50+
remoteNotification.getTitleLocalizationArgs(), remoteNotification.getTitle()));
51+
bundle.putString("message", getLocalizedString(remoteNotification.getBodyLocalizationKey(),
52+
remoteNotification.getBodyLocalizationArgs(), remoteNotification.getBody()));
5153
bundle.putString("sound", remoteNotification.getSound());
5254
bundle.putString("color", remoteNotification.getColor());
5355
bundle.putString("tag", remoteNotification.getTag());
@@ -178,4 +180,20 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
178180
pushNotificationHelper.sendToNotificationCentre(bundle);
179181
}
180182
}
183+
184+
private String getLocalizedString(String locKey, String[] locArgs, String defaultText) {
185+
String packageName = getPackageName();
186+
String result = defaultText;
187+
if (locKey != null) {
188+
int id = getResources().getIdentifier(locKey, "string", packageName);
189+
if (id != 0) {
190+
if (locArgs != null) {
191+
result = res.getString(id, (Object[]) locArgs);
192+
} else {
193+
result = res.getString(id);
194+
}
195+
}
196+
}
197+
return result;
198+
}
181199
}

0 commit comments

Comments
 (0)