diff --git a/docs/PAYLOAD.md b/docs/PAYLOAD.md index d06085340..679b28065 100644 --- a/docs/PAYLOAD.md +++ b/docs/PAYLOAD.md @@ -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 diff --git a/src/android/com/adobe/phonegap/push/FCMService.java b/src/android/com/adobe/phonegap/push/FCMService.java index db1738a72..8ae87410f 100644 --- a/src/android/com/adobe/phonegap/push/FCMService.java +++ b/src/android/com/adobe/phonegap/push/FCMService.java @@ -86,7 +86,6 @@ public void onMessageReceived(RemoteMessage message) { for (Map.Entry entry : message.getData().entrySet()) { extras.putString(entry.getKey(), entry.getValue()); } - if (extras != null && isAvailableSender(from)) { Context applicationContext = getApplicationContext(); @@ -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) { @@ -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 localeFormatData = new ArrayList(); - 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)); + } } } @@ -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; } } @@ -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; @@ -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); @@ -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 iterator = value.keySet().iterator(); diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 9b4656a00..c0cfe1879 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -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";