diff --git a/README.md b/README.md index 9ffc497f1..37bc3e00e 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Parameter | Description `options.android.sound` | `Boolean` Optional. If `true` it plays the sound specified in the push data or the default system sound. Default is `true`. `options.android.vibrate` | `Boolean` Optional. If `true` the device vibrates on receipt of notification. Default is `true`. `options.android.clearNotifications` | `Boolean` Optional. If `true` the app clears all pending notifications when it is closed. Default is `true`. +`options.android.forceShow` | `Boolean` Optional. If `true` will always show a notification, even when the app is on the foreground. Default is `false`. `options.ios` | `JSON Object` iOS specific initialization options. `options.ios.alert` | `Boolean` Optional. If `true` the device shows an alert on receipt of notification. Default is `false`. `options.ios.badge` | `Boolean` Optional. If `true` the device sets the badge number on receipt of notification. Default is `false`. diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index b22712d0a..8589ce5dc 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -87,24 +87,42 @@ protected void onMessage(Context context, Intent intent) { // Extract the payload from the message Bundle extras = intent.getExtras(); if (extras != null) { - // if we are in the foreground, just surface the payload, else post it to the statusbar - if (PushPlugin.isInForeground()) { + + SharedPreferences prefs = context.getSharedPreferences(PushPlugin.COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE); + boolean forceShow = prefs.getBoolean(FORCE_SHOW, false); + + // if we are in the foreground and forceShow is `false` only send data + if (!forceShow && PushPlugin.isInForeground()) { 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()) { + extras.putBoolean(FOREGROUND, true); + + showNotificationIfPossible(context, extras); + } + // if we are not in the foreground always send notification if the data has at least a message or title else { extras.putBoolean(FOREGROUND, false); - // Send a notification if there is a message - String message = this.getMessageText(extras); - String title = getString(extras, TITLE, ""); - if ((message != null && message.length() != 0) || - (title != null && title.length() != 0)) { - createNotification(context, extras); - } + showNotificationIfPossible(context, extras); } } } + + private void showNotificationIfPossible (Context context, Bundle extras) { + + // Send a notification if there is a message or title, otherwise just send data + String message = this.getMessageText(extras); + String title = getString(extras, TITLE, ""); + if ((message != null && message.length() != 0) || + (title != null && title.length() != 0)) { + createNotification(context, extras); + } else { + PushPlugin.sendExtras(extras); + } + } public void createNotification(Context context, Bundle extras) { NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -210,7 +228,7 @@ public void createNotification(Context context, Bundle extras) { setNotificationCount(extras, mBuilder); /* - * Notication add actions + * Notification add actions */ createActions(extras, mBuilder, resources, packageName); diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 09d7d4b9b..874b11968 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -41,4 +41,5 @@ public interface PushConstants { public static final String COUNT = "count"; public static final String FROM = "from"; public static final String COLLAPSE_KEY = "collapse_key"; + public static final String FORCE_SHOW = "forceShow"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index 9be4ce2d6..0714d1b15 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -80,6 +80,7 @@ public void run() { editor.putBoolean(SOUND, jo.optBoolean(SOUND, true)); editor.putBoolean(VIBRATE, jo.optBoolean(VIBRATE, true)); editor.putBoolean(CLEAR_NOTIFICATIONS, jo.optBoolean(CLEAR_NOTIFICATIONS, true)); + editor.putBoolean(FORCE_SHOW, jo.optBoolean(FORCE_SHOW, false)); editor.commit(); }