Commit 64da9a19 authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Merge pull request #178 from AdriVanHoudt/always-notify

allow forceShow option to always notify (android only atm)
parents 60a22d93 4c2f2249
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -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`\|`String` Optional. If `true`\|`"true"` the device shows an alert on receipt of notification. Default is `false`\|`"false"`.
`options.ios.badge` | `Boolean`\|`String` Optional. If `true`\|`"true"` the device sets the badge number on receipt of notification. Default is `false`\|`"false"`.
+28 −10
Original line number Diff line number Diff line
@@ -87,22 +87,40 @@ public class GCMIntentService extends GCMBaseIntentService implements PushConsta
        // 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
                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);
        }
    }

@@ -210,7 +228,7 @@ public class GCMIntentService extends GCMBaseIntentService implements PushConsta
        setNotificationCount(extras, mBuilder);

        /*
         * Notication add actions
         * Notification add actions
         */
        createActions(extras, mBuilder, resources, packageName);

+1 −0
Original line number Diff line number Diff line
@@ -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";
}
+1 −0
Original line number Diff line number Diff line
@@ -80,6 +80,7 @@ public class PushPlugin extends CordovaPlugin implements PushConstants {
                        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();
                    }