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

Commit

Permalink
Issue #299: 1.4.x not showing notification on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Nov 3, 2015
1 parent 1bac0d0 commit 9eae7b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,24 @@ public void onMessageReceived(String from, Bundle extras) {
SharedPreferences prefs = getApplicationContext().getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
boolean forceShow = prefs.getBoolean(FORCE_SHOW, false);

normalizeExtras(extras);
extras = normalizeExtras(extras);

// if we are in the foreground and forceShow is `false` only send data
if (!forceShow && PushPlugin.isInForeground()) {
Log.d(LOG_TAG, "foreground");
extras.putBoolean(FOREGROUND, true);
PushPlugin.sendExtras(extras);
}
// if we are in the foreground and forceShow is `true`, force show the notification if the data has at least a message or title
else if (forceShow && PushPlugin.isInForeground()) {
Log.d(LOG_TAG, "foreground force");
extras.putBoolean(FOREGROUND, true);

showNotificationIfPossible(getApplicationContext(), extras);
}
// if we are not in the foreground always send notification if the data has at least a message or title
else {
Log.d(LOG_TAG, "background");
extras.putBoolean(FOREGROUND, false);

showNotificationIfPossible(getApplicationContext(), extras);
Expand All @@ -88,18 +91,17 @@ else if (forceShow && PushPlugin.isInForeground()) {
/*
* Change a values key in the extras bundle
*/
private void replaceKey(String oldKey, String newKey, Bundle extras) {
private void replaceKey(String oldKey, String newKey, Bundle extras, Bundle newExtras) {
Object value = extras.get(oldKey);
if ( value != null ) {
extras.remove(oldKey);
if (value instanceof String) {
extras.putString(newKey, (String) value);
newExtras.putString(newKey, (String) value);
} else if (value instanceof Boolean) {
extras.putBoolean(newKey, (Boolean) value);
newExtras.putBoolean(newKey, (Boolean) value);
} else if (value instanceof Number) {
extras.putDouble(newKey, ((Number) value).doubleValue());
newExtras.putDouble(newKey, ((Number) value).doubleValue());
} else {
extras.putString(newKey, String.valueOf(value));
newExtras.putString(newKey, String.valueOf(value));
}
}
}
Expand All @@ -108,7 +110,7 @@ private void replaceKey(String oldKey, String newKey, Bundle extras) {
* Replace alternate keys with our canonical value
*/
private String normalizeKey(String key) {
if (key.equals(BODY) || key.equals(ALERT)) {
if (key.equals(BODY) || key.equals(ALERT) || key.equals(GCM_NOTIFICATION_BODY)) {
return MESSAGE;
} else if (key.equals(MSGCNT) || key.equals(BADGE)) {
return COUNT;
Expand All @@ -129,9 +131,11 @@ private String normalizeKey(String key) {
/*
* Parse bundle into normalized keys.
*/
private void normalizeExtras(Bundle extras) {
Log.d(LOG_TAG, "mormalize extras");
private Bundle normalizeExtras(Bundle extras) {
Log.d(LOG_TAG, "normalize extras");
Iterator<String> it = extras.keySet().iterator();
Bundle newExtras = new Bundle();

while (it.hasNext()) {
String key = it.next();

Expand All @@ -156,10 +160,8 @@ private void normalizeExtras(Bundle extras) {

String value = data.getString(jsonKey);
jsonKey = normalizeKey(jsonKey);
extras.putString(jsonKey, value);
newExtras.putString(jsonKey, value);
}

extras.remove(key);
}
} catch( JSONException e) {
Log.e(LOG_TAG, "normalizeExtras: JSON exception");
Expand All @@ -168,23 +170,31 @@ private void normalizeExtras(Bundle extras) {
}

String newKey = normalizeKey(key);
if ( !key.equals(newKey) ) {
Log.d(LOG_TAG, "replace key " + key + " with " + newKey);
replaceKey(key, newKey, extras);
}
Log.d(LOG_TAG, "replace key " + key + " with " + newKey);
replaceKey(key, newKey, extras, newExtras);

} // while

return newExtras;
}

private void showNotificationIfPossible (Context context, Bundle extras) {

// Send a notification if there is a message or title, otherwise just send data
String message = extras.getString(MESSAGE);
String title = extras.getString(TITLE);

Log.d(LOG_TAG, "message =[" + message + "]");
Log.d(LOG_TAG, "title =[" + title + "]");

if ((message != null && message.length() != 0) ||
(title != null && title.length() != 0)) {

Log.d(LOG_TAG, "create notification");

createNotification(context, extras);
} else {
Log.d(LOG_TAG, "send notification event");
PushPlugin.sendExtras(extras);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface PushConstants {
public static final String PICTURE = "picture";
public static final String GCM_N = "gcm.n.";
public static final String GCM_NOTIFICATION = "gcm.notification";
public static final String GCM_NOTIFICATION_BODY = "gcm.notification.body";
public static final String UA_PREFIX = "com.urbanairship.push";
public static final String PARSE_COM_DATA = "data";
public static final String ALERT = "alert";
Expand Down

0 comments on commit 9eae7b0

Please sign in to comment.