From 6ac6bb484fe042612875d38fa0cfd45784ec98b1 Mon Sep 17 00:00:00 2001 From: Steven Wexler Date: Tue, 22 Dec 2015 14:00:15 -0500 Subject: [PATCH] Fix null reference for notifications --- src/android/LocalNotification.java | 141 ++++++++++++++++++-------- src/android/notification/Builder.java | 2 +- 2 files changed, 97 insertions(+), 46 deletions(-) diff --git a/src/android/LocalNotification.java b/src/android/LocalNotification.java index fad61d59c..8123d30ba 100644 --- a/src/android/LocalNotification.java +++ b/src/android/LocalNotification.java @@ -25,6 +25,8 @@ import android.os.Build; +import com.hurdlr.core.TraxLog; + import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; @@ -60,8 +62,6 @@ public class LocalNotification extends CordovaPlugin { // Queues all events before deviceready private static ArrayList eventQueue = new ArrayList(); - public static JSONObject categories; - /** * Called after plugin construction and fields have been initialized. * Prefer to use pluginInitialize instead since there is no value in @@ -138,69 +138,48 @@ public void run() { if (action.equals("schedule")) { schedule(args); command.success(); - } - else if (action.equals("update")) { + } else if (action.equals("update")) { update(args); command.success(); - } - else if (action.equals("cancel")) { + } else if (action.equals("cancel")) { cancel(args); command.success(); - } - else if (action.equals("cancelAll")) { + } else if (action.equals("cancelAll")) { cancelAll(); command.success(); - } - else if (action.equals("clear")) { + } else if (action.equals("clear")) { clear(args); command.success(); - } - else if (action.equals("clearAll")) { + } else if (action.equals("clearAll")) { clearAll(); command.success(); - } - else if (action.equals("isPresent")) { + } else if (action.equals("isPresent")) { isPresent(args.optInt(0), command); - } - else if (action.equals("isScheduled")) { + } else if (action.equals("isScheduled")) { isScheduled(args.optInt(0), command); - } - else if (action.equals("isTriggered")) { + } else if (action.equals("isTriggered")) { isTriggered(args.optInt(0), command); - } - else if (action.equals("getAllIds")) { + } else if (action.equals("getAllIds")) { getAllIds(command); - } - else if (action.equals("getScheduledIds")) { + } else if (action.equals("getScheduledIds")) { getScheduledIds(command); - } - else if (action.equals("getTriggeredIds")) { + } else if (action.equals("getTriggeredIds")) { getTriggeredIds(command); - } - else if (action.equals("getSingle")) { + } else if (action.equals("getSingle")) { getSingle(args, command); - } - else if (action.equals("getSingleScheduled")) { + } else if (action.equals("getSingleScheduled")) { getSingleScheduled(args, command); - } - else if (action.equals("getSingleTriggered")) { + } else if (action.equals("getSingleTriggered")) { getSingleTriggered(args, command); - } - else if (action.equals("getAll")) { + } else if (action.equals("getAll")) { getAll(args, command); - } - else if (action.equals("getScheduled")) { + } else if (action.equals("getScheduled")) { getScheduled(args, command); - } - else if (action.equals("getTriggered")) { + } else if (action.equals("getTriggered")) { getTriggered(args, command); - } - else if (action.equals("deviceready")) { + } else if (action.equals("deviceready")) { deviceready(); } - else if (action.equals("registerCategories")) { - registerCategories(args); - } } }); @@ -549,9 +528,13 @@ static void fireEvent (String event, Notification notification, String action) { "\"" + event + "\"," + params + ")"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - webView.sendJavascript(js); + if (webView != null) { + webView.sendJavascript(js); + } } else { - webView.loadUrl("javascript:" + js); + if (webView != null) { + webView.loadUrl("javascript:" + js); + } } } @@ -604,7 +587,75 @@ private Manager getNotificationMgr() { return Manager.getInstance(cordova.getActivity()); } - private void registerCategories(JSONArray data) { - categories = data.optJSONObject(0); + public static JSONObject getCategories() { + try { + JSONObject read = new JSONObject(); + read.put("identifier", "READ"); + read.put("title", "Read"); + read.put("activationMode", "foreground"); + read.put("destructive", false); + read.put("authenticationRequired", true); + read.put("icon", android.R.drawable.ic_menu_view); + + JSONObject ignore = new JSONObject(); + ignore.put("identifier", "IGNORE"); + ignore.put("title", "Ignore"); + ignore.put("activationMode", "foreground"); + ignore.put("destructive", false); + ignore.put("authenticationRequired", false); + ignore.put("icon", android.R.drawable.ic_menu_close_clear_cancel); + + JSONObject delete = new JSONObject(); + delete.put("identifier", "DELETE"); + delete.put("title", "Delete"); + delete.put("activationMode", "background"); + delete.put("destructive", true); + delete.put("authenticationRequired", true); + delete.put("icon", android.R.drawable.ic_menu_delete); + + JSONObject postRetry = new JSONObject(); + postRetry.put("identifier", "POST_RETRY"); + postRetry.put("title", "RETRY"); + postRetry.put("activationMode", "background"); + postRetry.put("destructive", false); + postRetry.put("authenticationRequired", true); + postRetry.put("icon", android.R.drawable.ic_menu_upload); + + JSONObject postCancel = new JSONObject(); + postCancel.put("identifier", "POST_CANCEL"); + postCancel.put("title", "CANCEL"); + postCancel.put("activationMode", "background"); + postCancel.put("destructive", false); + postCancel.put("authenticationRequired", true); + postCancel.put("icon", android.R.drawable.ic_menu_close_clear_cancel); + + + JSONObject readIgnore = new JSONObject(); + JSONArray readIgnoreArr = new JSONArray(); + readIgnoreArr.put(read); + readIgnoreArr.put(ignore); + readIgnore.put("actions", readIgnoreArr); + + JSONObject readDelete = new JSONObject(); + JSONArray readDeleteArr = new JSONArray(); + readDeleteArr.put(read); + readDeleteArr.put(delete); + readDelete.put("actions", readDeleteArr); + + JSONObject postRetryCancel = new JSONObject(); + JSONArray postRetryCancelArr = new JSONArray(); + postRetryCancelArr.put(postRetry); + postRetryCancelArr.put(postCancel); + postRetryCancel.put("actions", postRetryCancelArr); + + JSONObject categories = new JSONObject(); + categories.put("READ_IGNORE", readIgnore); + categories.put("READ_DELETE", readDelete); + categories.put("POST_RETRY_CANCEL", postRetryCancel); + return categories; + } catch (JSONException e) { + TraxLog.e(e); + return null; + } } } diff --git a/src/android/notification/Builder.java b/src/android/notification/Builder.java index b89e50347..7223e467b 100644 --- a/src/android/notification/Builder.java +++ b/src/android/notification/Builder.java @@ -200,7 +200,7 @@ private void applyContentReceiver(NotificationCompat.Builder builder) { private NotificationCompat.Builder addActions(NotificationCompat.Builder builder) { String cat_id = options.getCategory(); if(!cat_id.equals("NO_BUTTONS")) { - JSONObject category = LocalNotification.categories.optJSONObject(cat_id); + JSONObject category = LocalNotification.getCategories().optJSONObject(cat_id); JSONArray actions = category.optJSONArray("actions"); Intent intent; PendingIntent contentIntent;