Commit 9a6d89b5 by Sebastián Katzer

Added back update method for Android

parent 4fe4e786
...@@ -157,10 +157,10 @@ public class LocalNotification extends CordovaPlugin { ...@@ -157,10 +157,10 @@ public class LocalNotification extends CordovaPlugin {
schedule(args); schedule(args);
command.success(); command.success();
} else } else
// if (action.equals("update")) { if (action.equals("update")) {
// update(args); update(args);
// command.success(); command.success();
// } else } else
if (action.equals("cancel")) { if (action.equals("cancel")) {
cancel(args); cancel(args);
command.success(); command.success();
...@@ -290,26 +290,26 @@ public class LocalNotification extends CordovaPlugin { ...@@ -290,26 +290,26 @@ public class LocalNotification extends CordovaPlugin {
} }
} }
// /** /**
// * Update multiple local notifications. * Update multiple local notifications.
// * *
// * @param updates * @param updates
// * Notification properties including their IDs * Notification properties including their IDs
// */ */
// private void update (JSONArray updates) { private void update (JSONArray updates) {
// for (int i = 0; i < updates.length(); i++) { for (int i = 0; i < updates.length(); i++) {
// JSONObject update = updates.optJSONObject(i); JSONObject update = updates.optJSONObject(i);
// int id = update.optInt("id", 0); int id = update.optInt("id", 0);
// Notification notification = Notification notification =
// getNotMgr().update(id, update, TriggerReceiver.class); getNotMgr().update(id, update, TriggerReceiver.class);
// if (notification == null) if (notification == null)
// continue; continue;
// fireEvent("update", notification); fireEvent("update", notification);
// } }
// } }
/** /**
* Cancel multiple local notifications. * Cancel multiple local notifications.
......
...@@ -45,14 +45,18 @@ public class TriggerReceiver extends AbstractTriggerReceiver { ...@@ -45,14 +45,18 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
*/ */
@Override @Override
public void onTrigger (Notification notification, Bundle bundle) { public void onTrigger (Notification notification, Bundle bundle) {
int badge = notification.getOptions().getBadgeNumber(); boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
int badge = notification.getOptions().getBadgeNumber();
if (badge > 0) { if (badge > 0) {
Manager.getInstance(notification.getContext()).setBadge(badge); Manager.getInstance(notification.getContext()).setBadge(badge);
} }
notification.show(); notification.show();
LocalNotification.fireEvent("trigger", notification);
if (!isUpdate) {
LocalNotification.fireEvent("trigger", notification);
}
} }
/** /**
......
...@@ -120,33 +120,23 @@ public final class Manager { ...@@ -120,33 +120,23 @@ public final class Manager {
mgr.createNotificationChannel(channel); mgr.createNotificationChannel(channel);
} }
// /** /**
// * Clear local notification specified by ID. * Update local notification specified by ID.
// * *
// * @param id * @param id The notification ID.
// * The notification ID * @param updates JSON object with notification options.
// * @param updates * @param receiver Receiver to handle the trigger event.
// * JSON object with notification options */
// * @param receiver public Notification update (int id, JSONObject updates, Class<?> receiver) {
// * Receiver to handle the trigger event Notification notification = get(id);
// */
// public Notification update (int id, JSONObject updates, Class<?> receiver) { if (notification == null)
// Notification notification = get(id); return null;
// if (notification == null) notification.update(updates, receiver);
// return null;
return notification;
// notification.cancel(); }
// JSONObject options = mergeJSONObjects(
// notification.getOptions().getDict(), updates);
// try {
// options.put("updated", true);
// } catch (JSONException ignore) {}
// return schedule(options, receiver);
// }
/** /**
* Clear local notification specified by ID. * Clear local notification specified by ID.
...@@ -386,30 +376,6 @@ public final class Manager { ...@@ -386,30 +376,6 @@ public final class Manager {
return new Notification(context, options); return new Notification(context, options);
} }
// /**
// * Merge two JSON objects.
// *
// * @param obj1
// * JSON object
// * @param obj2
// * JSON object with new options
// */
// private JSONObject mergeJSONObjects (JSONObject obj1, JSONObject obj2) {
// Iterator it = obj2.keys();
// while (it.hasNext()) {
// try {
// String key = (String)it.next();
// obj1.put(key, obj2.opt(key));
// } catch (JSONException e) {
// e.printStackTrace();
// }
// }
// return obj1;
// }
/** /**
* Set the badge number of the app icon. * Set the badge number of the app icon.
* *
...@@ -444,7 +410,7 @@ public final class Manager { ...@@ -444,7 +410,7 @@ public final class Manager {
private NotificationManagerCompat getNotCompMgr() { private NotificationManagerCompat getNotCompMgr() {
return NotificationManagerCompat.from(context); return NotificationManagerCompat.from(context);
} }
/** /**
* Notification manager compat for the application. * Notification manager compat for the application.
*/ */
......
...@@ -39,6 +39,7 @@ import org.json.JSONObject; ...@@ -39,6 +39,7 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -62,6 +63,9 @@ public final class Notification { ...@@ -62,6 +63,9 @@ public final class Notification {
// Extra key for the id // Extra key for the id
public static final String EXTRA_ID = "NOTIFICATION_ID"; public static final String EXTRA_ID = "NOTIFICATION_ID";
// Extra key for the update flag
public static final String EXTRA_UPDATE = "NOTIFICATION_UPDATE";
// Key for private preferences // Key for private preferences
static final String PREF_KEY_ID = "NOTIFICATION_ID"; static final String PREF_KEY_ID = "NOTIFICATION_ID";
...@@ -288,6 +292,27 @@ public final class Notification { ...@@ -288,6 +292,27 @@ public final class Notification {
} }
/** /**
* Update the notification properties.
*
* @param updates The properties to update.
* @param receiver Receiver to handle the trigger event.
*/
void update (JSONObject updates, Class<?> receiver) {
mergeJSONObjects(updates);
persist(null);
if (getType() != Type.TRIGGERED)
return;
Intent intent = new Intent(context, receiver)
.setAction(PREF_KEY_ID + options.getId())
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Notification.EXTRA_UPDATE, true);
trigger(intent, receiver);
}
/**
* Encode options to JSON. * Encode options to JSON.
*/ */
public String toString() { public String toString() {
...@@ -318,6 +343,9 @@ public final class Notification { ...@@ -318,6 +343,9 @@ public final class Notification {
editor.putString(id, options.toString()); editor.putString(id, options.toString());
editor.apply(); editor.apply();
if (ids == null)
return;
editor = getPrefs(PREF_KEY_PID).edit(); editor = getPrefs(PREF_KEY_PID).edit();
editor.putStringSet(id, ids); editor.putStringSet(id, ids);
editor.apply(); editor.apply();
...@@ -355,6 +383,23 @@ public final class Notification { ...@@ -355,6 +383,23 @@ public final class Notification {
} }
/** /**
* Merge two JSON objects.
*/
private void mergeJSONObjects (JSONObject updates) {
JSONObject dict = options.getDict();
Iterator it = updates.keys();
while (it.hasNext()) {
try {
String key = (String)it.next();
dict.put(key, updates.opt(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
/**
* Shared private preferences for the application. * Shared private preferences for the application.
*/ */
private SharedPreferences getPrefs (String key) { private SharedPreferences getPrefs (String key) {
......
...@@ -31,7 +31,6 @@ import android.support.v4.media.session.MediaSessionCompat; ...@@ -31,7 +31,6 @@ import android.support.v4.media.session.MediaSessionCompat;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONArray;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
......
...@@ -164,12 +164,11 @@ exports.convertProperties = function (options) { ...@@ -164,12 +164,11 @@ exports.convertProperties = function (options) {
* @return [ Map ] Interaction object with category & actions. * @return [ Map ] Interaction object with category & actions.
*/ */
exports.convertActions = function (options) { exports.convertActions = function (options) {
var actions = [];
if (!options.actions) if (!options.actions)
return null; return null;
var actions = [];
for (var action of options.actions) { for (var action of options.actions) {
if (!action.id) { if (!action.id) {
...@@ -200,6 +199,9 @@ exports.convertTrigger = function (options) { ...@@ -200,6 +199,9 @@ exports.convertTrigger = function (options) {
var trigger = options.trigger || {}, var trigger = options.trigger || {},
date = this.getValueFor(trigger, 'at', 'firstAt', 'date'); date = this.getValueFor(trigger, 'at', 'firstAt', 'date');
if (!options.trigger)
return;
if (!trigger.type) { if (!trigger.type) {
trigger.type = trigger.center ? 'location' : 'calendar'; trigger.type = trigger.center ? 'location' : 'calendar';
} }
...@@ -262,6 +264,9 @@ exports.convertProgressBar = function (options) { ...@@ -262,6 +264,9 @@ exports.convertProgressBar = function (options) {
var isAndroid = device.platform == 'Android', var isAndroid = device.platform == 'Android',
cfg = options.progressBar; cfg = options.progressBar;
if (cfg === undefined)
return;
if (typeof cfg === 'boolean') { if (typeof cfg === 'boolean') {
cfg = options.progressBar = { enabled: cfg }; cfg = options.progressBar = { enabled: cfg };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment