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

Commit

Permalink
Issue #62: "Actionable" Notification Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Aug 14, 2015
1 parent 83251a2 commit 24ae490
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
32 changes: 31 additions & 1 deletion src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URL;
import java.util.Random;

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

Expand Down Expand Up @@ -176,7 +177,12 @@ public void createNotification(Context context, Bundle extras) {
* Notification count
*/
setNotificationCount(extras, mBuilder);


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

int notId = 0;

try {
Expand All @@ -192,6 +198,30 @@ public void createNotification(Context context, Bundle extras) {
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) {
Expand Down
13 changes: 7 additions & 6 deletions src/android/com/adobe/phonegap/push/PushHandlerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(LOG_TAG, "onCreate");

boolean isPushPluginActive = PushPlugin.isActive();
processPushBundle(isPushPluginActive);
boolean isPushPluginActive = PushPlugin.isActive();
processPushBundle(isPushPluginActive);

finish();
finish();

if (!isPushPluginActive) {
forceMainActivityReload();
}
if (!isPushPluginActive) {
forceMainActivityReload();
}
}

/**
Expand All @@ -45,6 +45,7 @@ private void processPushBundle(boolean isPushPluginActive) {

originalExtras.putBoolean("foreground", false);
originalExtras.putBoolean("coldstart", !isPushPluginActive);
originalExtras.putString("callback", getIntent().getExtras().getString("callback"));

PushPlugin.sendExtras(originalExtras);
}
Expand Down
3 changes: 3 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -194,6 +195,8 @@ else if (key.equals("coldstart")){
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;
Expand Down
12 changes: 12 additions & 0 deletions www/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 24ae490

Please sign in to comment.