Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Localization with FCM Admin (Node.js) #2924

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ Or use localization with formatted constants.
}
```

Or use localization compatible with FCM Admin package for Node.js (Android only)
```json
{
"android": {
"data": {
"title_loc_key": "NOTIFICATION_TITLE",
"body_loc_key": "NOTIFICATION_MESSAGE",
"body_loc_args": "[\"args\", \"as\", \"stringified\", \"array\"]",
"title_loc_args": "[\"args\", \"as\", \"stringified\", \"array\"]"
}
},
"tokens": ["REGISTRARION_ID_1", "REGISTRATION_ID_2"]
}
```

Here is an example using fcm-node that sends the above JSON:

```javascript
Expand Down
59 changes: 42 additions & 17 deletions src/android/com/adobe/phonegap/push/FCMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public void onMessageReceived(RemoteMessage message) {
for (Map.Entry<String, String> entry : message.getData().entrySet()) {
extras.putString(entry.getKey(), entry.getValue());
}

if (extras != null && isAvailableSender(from)) {
Context applicationContext = getApplicationContext();

Expand All @@ -96,7 +95,6 @@ public void onMessageReceived(RemoteMessage message) {
boolean clearBadge = prefs.getBoolean(CLEAR_BADGE, false);
String messageKey = prefs.getString(MESSAGE_KEY, MESSAGE);
String titleKey = prefs.getString(TITLE_KEY, TITLE);

extras = normalizeExtras(applicationContext, extras, messageKey, titleKey);

if (clearBadge) {
Expand Down Expand Up @@ -149,22 +147,36 @@ private void replaceKey(Context context, String oldKey, String newKey, Bundle ex
}
}

private String localizeKey(Context context, String key, String value) {
return localizeKey(context, key, value, "");
}

/*
* Normalize localization for key
*/
private String localizeKey(Context context, String key, String value) {
private String localizeKey(Context context, String key, String value, String args) {
if (key.equals(TITLE) || key.equals(MESSAGE) || key.equals(SUMMARY_TEXT)) {
try {
JSONObject localeObject = new JSONObject(value);

String localeKey = localeObject.getString(LOC_KEY);

String localeKey;
ArrayList<String> localeFormatData = new ArrayList<String>();
if (!localeObject.isNull(LOC_DATA)) {
String localeData = localeObject.getString(LOC_DATA);
JSONArray localeDataArray = new JSONArray(localeData);
for (int i = 0; i < localeDataArray.length(); i++) {
localeFormatData.add(localeDataArray.getString(i));

if(value.startsWith("{")) {
JSONObject localeObject = new JSONObject(value);
localeKey = localeObject.getString(LOC_KEY);
if (!localeObject.isNull(LOC_DATA)) {
String localeData = localeObject.getString(LOC_DATA);
JSONArray localeDataArray = new JSONArray(localeData);
for (int i = 0; i < localeDataArray.length(); i++) {
localeFormatData.add(localeDataArray.getString(i));
}
}
} else {
localeKey = value;
if(!args.isEmpty() && args.startsWith("[")){
JSONArray localeDataArray = new JSONArray(args);
for (int i = 0; i < localeDataArray.length(); i++) {
localeFormatData.add(localeDataArray.getString(i));
}
}
}

Expand All @@ -177,12 +189,13 @@ private String localizeKey(Context context, String key, String value) {
return resources.getString(resourceId, localeFormatData.toArray());
} else {
Log.d(LOG_TAG, "can't find resource for locale key = " + localeKey);

return value;
}
} catch (JSONException e) {
Log.d(LOG_TAG, "no locale found for key = " + key + ", error " + e.getMessage());

return value;
} catch(Exception e) {
Log.e(LOG_TAG, "error during string localization: " + e.toString());
return value;
}
}
Expand All @@ -195,9 +208,9 @@ private String localizeKey(Context context, String key, String value) {
*/
private String normalizeKey(String key, String messageKey, String titleKey, Bundle newExtras) {
if (key.equals(BODY) || key.equals(ALERT) || key.equals(MP_MESSAGE) || key.equals(GCM_NOTIFICATION_BODY)
|| key.equals(TWILIO_BODY) || key.equals(messageKey) || key.equals(AWS_PINPOINT_BODY)) {
|| key.equals(TWILIO_BODY) || key.equals(BODY_LOC_KEY) || key.equals(messageKey) || key.equals(AWS_PINPOINT_BODY)) {
return MESSAGE;
} else if (key.equals(TWILIO_TITLE) || key.equals(SUBJECT) || key.equals(titleKey)) {
} else if (key.equals(TWILIO_TITLE) || key.equals(SUBJECT) || key.equals(TITLE_LOC_KEY) || key.equals(titleKey)) {
return TITLE;
} else if (key.equals(MSGCNT) || key.equals(BADGE)) {
return COUNT;
Expand Down Expand Up @@ -233,7 +246,7 @@ private Bundle normalizeExtras(Context context, Bundle extras, String messageKey

Log.d(LOG_TAG, "key = " + key);

// If normalizeKeythe key is "data" or "message" and the value is a json object extract
// If normalizeKey the key is "data" or "message" and the value is a json object extract
// This is to support parse.com and other services. Issue #147 and pull #218
if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE) || key.equals(messageKey)) {
Object json = extras.get(key);
Expand Down Expand Up @@ -270,6 +283,18 @@ private Bundle normalizeExtras(Context context, Bundle extras, String messageKey
Log.d(LOG_TAG, "replace key " + key + " with " + newKey);
replaceKey(context, key, newKey, extras, newExtras);
}
} else if(key.equals(TITLE_LOC_KEY)) {
String value = extras.getString(key);
String newKey = normalizeKey(key, messageKey, titleKey, newExtras);
String args = extras.containsKey(TITLE_LOC_DATA) ? extras.getString(TITLE_LOC_DATA) : "";
value = localizeKey(context, newKey, value, args);
newExtras.putString(newKey, value);
} else if(key.equals(BODY_LOC_KEY)) {
String value = extras.getString(key);
String newKey = normalizeKey(key, messageKey, titleKey, newExtras);
String args = extras.containsKey(BODY_LOC_DATA) ? extras.getString(BODY_LOC_DATA) : "";
value = localizeKey(context, newKey, value, args);
newExtras.putString(newKey, value);
} else if (key.equals(("notification"))) {
Bundle value = extras.getBundle(key);
Iterator<String> iterator = value.keySet().iterator();
Expand Down
4 changes: 4 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public interface PushConstants {
public static final String INLINE_REPLY_LABEL = "replyLabel";
public static final String LOC_KEY = "locKey";
public static final String LOC_DATA = "locData";
public static final String TITLE_LOC_KEY = "title_loc_key";
public static final String TITLE_LOC_DATA = "title_loc_args";
public static final String BODY_LOC_KEY = "body_loc_key";
public static final String BODY_LOC_DATA = "body_loc_args";
public static final String TWILIO_BODY = "twi_body";
public static final String TWILIO_TITLE = "twi_title";
public static final String TWILIO_SOUND = "twi_sound";
Expand Down