Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null reference for notifications #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 96 additions & 45 deletions src/android/LocalNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,8 +62,6 @@ public class LocalNotification extends CordovaPlugin {
// Queues all events before deviceready
private static ArrayList<String> eventQueue = new ArrayList<String>();

public static JSONObject categories;

/**
* Called after plugin construction and fields have been initialized.
* Prefer to use pluginInitialize instead since there is no value in
Expand Down Expand Up @@ -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);
}
}
});

Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/android/notification/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down