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

Commit a4c6459

Browse files
author
Boris Tacyniak
authored
Merge pull request #1837 from plus-/master
Handle localization for notification title and body
2 parents 4fa8ac1 + 8161e1b commit a4c6459

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.os.Bundle;
1010
import android.os.Handler;
1111
import android.os.Looper;
12+
import android.content.Context;
1213
import android.util.Log;
1314
import android.net.Uri;
1415
import androidx.annotation.NonNull;
@@ -46,8 +47,11 @@ public void handleReceivedMessage(RemoteMessage message) {
4647
// ^ It's null when message is from GCM
4748
RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());
4849

49-
bundle.putString("title", remoteNotification.getTitle());
50-
bundle.putString("message", remoteNotification.getBody());
50+
String title = getLocalizedString(remoteNotification.getTitle(), remoteNotification.getTitleLocalizationKey(), remoteNotification.getTitleLocalizationArgs());
51+
String body = getLocalizedString(remoteNotification.getBody(), remoteNotification.getBodyLocalizationKey(), remoteNotification.getBodyLocalizationArgs());
52+
53+
bundle.putString("title", title);
54+
bundle.putString("message", body);
5155
bundle.putString("sound", remoteNotification.getSound());
5256
bundle.putString("color", remoteNotification.getColor());
5357
bundle.putString("tag", remoteNotification.getTag());
@@ -178,4 +182,28 @@ private void handleRemotePushNotification(ReactApplicationContext context, Bundl
178182
pushNotificationHelper.sendToNotificationCentre(bundle);
179183
}
180184
}
185+
186+
private String getLocalizedString(String text, String locKey, String[] locArgs) {
187+
if(text != null) {
188+
return text;
189+
}
190+
191+
Context context = mFirebaseMessagingService.getApplicationContext();
192+
String packageName = context.getPackageName();
193+
194+
String result = null;
195+
196+
if (locKey != null) {
197+
int id = context.getResources().getIdentifier(locKey, "string", packageName);
198+
if (id != 0) {
199+
if (locArgs != null) {
200+
result = context.getResources().getString(id, (Object[]) locArgs);
201+
} else {
202+
result = context.getResources().getString(id);
203+
}
204+
}
205+
}
206+
207+
return result;
208+
}
181209
}

0 commit comments

Comments
 (0)