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

Commit

Permalink
Ability to use custom keys to find message title and text on Android (#…
Browse files Browse the repository at this point in the history
…1604)

* Added ability to use custom keys to find message title and text on Android

* Fix api reference for Android

* Fix default values for message keys on Android. Fix api reference for this changes. Fix keys usage in some case.
  • Loading branch information
eKazim authored and macdonst committed Mar 1, 2017
1 parent 8c93f86 commit fd36629
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Attribute | Type | Default | Description
`android.clearNotifications` | `boolean` | `true` | Optional. If `true` the app clears all pending notifications when it is closed.
`android.forceShow` | `boolean` | `false` | Optional. Controls the behavior of the notification when app is in foreground. If `true` and app is in foreground, it will show a notification in the notification drawer, the same way as when the app is in background (and `on('notification')` callback will be called *only when the user clicks the notification*). When `false` and app is in foreground, the `on('notification')` callback will be called immediately.
`android.topics` | `array` | `[]` | Optional. If the array contains one or more strings each string will be used to subscribe to a GcmPubSub topic. Note: you should omit the `/topics/` prefix from each element of the array as the plugin will handle that for you.
`android.messageKey` | `string` | `message` | Optional. The key to search for text of notification.
`android.titleKey` | `string` | `'title'` | Optional. The key to search for title of notification.

#### Browser

Expand Down
23 changes: 13 additions & 10 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public void onMessageReceived(String from, Bundle extras) {
SharedPreferences prefs = applicationContext.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);
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);
extras = normalizeExtras(applicationContext, extras, messageKey, titleKey);

if (clearBadge) {
PushPlugin.setApplicationIconBadgeNumber(getApplicationContext(), 0);
Expand Down Expand Up @@ -167,10 +169,10 @@ private String localizeKey(Context context, String key, String value) {
/*
* Replace alternate keys with our canonical value
*/
private String normalizeKey(String key) {
if (key.equals(BODY) || key.equals(ALERT) || key.equals(MP_MESSAGE) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY)) {
private String normalizeKey(String key, String messageKey, String titleKey) {
if (key.equals(BODY) || key.equals(ALERT) || key.equals(MP_MESSAGE) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY) || key.equals(messageKey)) {
return MESSAGE;
} else if (key.equals(TWILIO_TITLE)) {
} else if (key.equals(TWILIO_TITLE) || key.equals(titleKey)) {
return TITLE;
}else if (key.equals(MSGCNT) || key.equals(BADGE)) {
return COUNT;
Expand All @@ -191,7 +193,7 @@ private String normalizeKey(String key) {
/*
* Parse bundle into normalized keys.
*/
private Bundle normalizeExtras(Context context, Bundle extras) {
private Bundle normalizeExtras(Context context, Bundle extras, String messageKey, String titleKey) {
Log.d(LOG_TAG, "normalize extras");
Iterator<String> it = extras.keySet().iterator();
Bundle newExtras = new Bundle();
Expand All @@ -203,23 +205,24 @@ private Bundle normalizeExtras(Context context, Bundle extras) {

// If normalizeKeythe 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)) {
if (key.equals(PARSE_COM_DATA) || key.equals(MESSAGE) || key.equals(messageKey)) {
Object json = extras.get(key);
// Make sure data is json object stringified
if ( json instanceof String && ((String) json).startsWith("{") ) {
Log.d(LOG_TAG, "extracting nested message data from key = " + key);
try {
// If object contains message keys promote each value to the root of the bundle
JSONObject data = new JSONObject((String) json);
if ( data.has(ALERT) || data.has(MESSAGE) || data.has(BODY) || data.has(TITLE) ) {
if ( data.has(ALERT) || data.has(MESSAGE) || data.has(BODY) || data.has(TITLE) ||
data.has(messageKey) || data.has(titleKey) ) {
Iterator<String> jsonIter = data.keys();
while (jsonIter.hasNext()) {
String jsonKey = jsonIter.next();

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

String value = data.getString(jsonKey);
jsonKey = normalizeKey(jsonKey);
jsonKey = normalizeKey(jsonKey, messageKey, titleKey);
value = localizeKey(context, jsonKey, value);

newExtras.putString(jsonKey, value);
Expand All @@ -236,7 +239,7 @@ private Bundle normalizeExtras(Context context, Bundle extras) {
String notifkey = iterator.next();

Log.d(LOG_TAG, "notifkey = " + notifkey);
String newKey = normalizeKey(notifkey);
String newKey = normalizeKey(notifkey, messageKey, titleKey);
Log.d(LOG_TAG, "replace key " + notifkey + " with " + newKey);

String valueData = value.getString(notifkey);
Expand All @@ -247,7 +250,7 @@ private Bundle normalizeExtras(Context context, Bundle extras) {
continue;
}

String newKey = normalizeKey(key);
String newKey = normalizeKey(key, messageKey, titleKey);
Log.d(LOG_TAG, "replace key " + key + " with " + newKey);
replaceKey(context, key, newKey, extras, newExtras);

Expand Down
2 changes: 2 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ public interface PushConstants {
public static final String MP_MESSAGE = "mp_message";
public static final String START_IN_BACKGROUND = "cdvStartInBackground";
public static final String FORCE_START = "force-start";
public static final String MESSAGE_KEY = "messageKey";
public static final String TITLE_KEY = "titleKey";
}
2 changes: 2 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public void run() {
editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true));
editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false));
editor.putString(SENDER_ID, senderID);
editor.putString(MESSAGE_KEY, jo.optString(MESSAGE_KEY));
editor.putString(TITLE_KEY, jo.optString(TITLE_KEY));
editor.commit();

}
Expand Down

0 comments on commit fd36629

Please sign in to comment.