Commit 45af9530 authored by Simon MacDonald's avatar Simon MacDonald
Browse files

Issue #62: "Actionable" Notification Buttons

parent ecf103b3
Loading
Loading
Loading
Loading
+21 −0
Changes for README.md: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -313,6 +313,27 @@ and:

You will only see both "Push number 1" and "Push number 2" in the shade.

### Action Buttons

Your notification can include action buttons. If you wish to include an icon along with the button name they must be placed in the `res/drawable` directory of your Android project. Then you can send the following JSON from GCM:

```javascript
{
	title:"AUX Scrum", 
	message: "Scrum: Daily touchbase @ 10am Please be on time so we can cover everything on the agenda.", 
	actions: [
		{ icon: "emailGuests", title: "EMAIL GUESTS", callback: "app.emailGuests"},
		{ icon: "snooze", title: "SNOOZE", callback: "app.snooze"},
	]
}
```

This will produce the following notification in your tray:

![action_combo](https://cloud.githubusercontent.com/assets/353180/9313435/02554d2a-44f1-11e5-8cd9-0aadd1e02b18.png)

If your users clicks on the main body of the notification your app will be opened. However if they click on either of the action buttons the app will open (or start) and the specified JavaScript callback will be executed. In this case it is `app.emailGuests` and `app.snooze` respectively.

## iOS Behaviour

### Sound
+30 −0
Changes for src/android/com/adobe/phonegap/push/GCMIntentService.java: 30 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

@@ -177,6 +178,11 @@ public class GCMIntentService extends GCMBaseIntentService {
         */
        setNotificationCount(extras, mBuilder);

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

        int notId = 0;
        
        try {
@@ -192,6 +198,30 @@ public class GCMIntentService extends GCMBaseIntentService {
        mNotificationManager.notify((String) appName, notId, mBuilder.build());
    }

    private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, Resources resources, String packageName) {
        Log.d(LOG_TAG, "create actions");
        String actions = extras.getString("actions");
        if (actions != null) {
            try {
                JSONArray actionsArray = new JSONArray(actions);
                for (int i=0; i < actionsArray.length(); i++) {
                    Log.d(LOG_TAG, "adding action");
                    JSONObject action = actionsArray.getJSONObject(i);
                    Log.d(LOG_TAG, "adding callback = " + action.getString("callback"));
                    Intent intent = new Intent(this, PushHandlerActivity.class);
                    intent.putExtra("callback", action.getString("callback"));
                    intent.putExtra("pushBundle", extras);
                    PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, 0);

                    mBuilder.addAction(resources.getIdentifier(action.getString("icon"), "drawable", packageName),
                            action.getString("title"), pIntent);
                }
            } catch(JSONException e) {
                // nope
            }
        }
    }

    private void setNotificationCount(Bundle extras, NotificationCompat.Builder mBuilder) {
        String msgcnt = extras.getString("msgcnt");
        if (msgcnt == null) {
+1 −0
Changes for src/android/com/adobe/phonegap/push/PushHandlerActivity.java: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ public class PushHandlerActivity extends Activity
            
            originalExtras.putBoolean("foreground", false);
            originalExtras.putBoolean("coldstart", !isPushPluginActive);
            originalExtras.putString("callback", getIntent().getExtras().getString("callback"));

			PushPlugin.sendExtras(originalExtras);
		}
+3 −0
Changes for src/android/com/adobe/phonegap/push/PushPlugin.java: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ public class PushPlugin extends CordovaPlugin {
    private static String gSenderID;
    private static Bundle gCachedExtras = null;
    private static boolean gForeground = false;
    private static String gCallback = null;

    /**
     * Gets the application context from cordova's main activity.
@@ -200,6 +201,8 @@ public class PushPlugin extends CordovaPlugin {
                    json.put("sound", value);
                } else if (key.equals("image")) {
                    json.put("image", value);
                } else if (key.equals("callback")) {
                    json.put("callback", value);
                }
                else if ( value instanceof String ) {
                    String strValue = (String)value;
+12 −0
Changes for www/push.js: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -33,6 +33,18 @@ var PushNotification = function(options) {
    var success = function(result) {
        if (result && typeof result.registrationId !== 'undefined') {
            that.emit('registration', result);
        } else if (result && typeof result.callback !== 'undefined') {
            var executeFunctionByName = function(functionName, context /*, args */) {
                var args = Array.prototype.slice.call(arguments, 2);
                var namespaces = functionName.split(".");
                var func = namespaces.pop();
                for (var i = 0; i < namespaces.length; i++) {
                    context = context[namespaces[i]];
                }
                return context[func].apply(context, args);
            }
            
            executeFunctionByName(result.callback, window, result);
        } else if (result && typeof result.message !== 'undefined') {
            that.emit('notification', result);
        }