Commit 01ffae10 by Sebastián Katzer

Update example

parent aa24cd44
...@@ -10,8 +10,8 @@ ...@@ -10,8 +10,8 @@
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name="de.appplant.cordova.plugin.background.ForegroundService" /> <service android:name="de.appplant.cordova.plugin.background.ForegroundService" />
<receiver android:name="de.appplant.cordova.plugin.localnotification.Receiver" /> <receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.Receiver" />
<receiver android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" /> <receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" />
<receiver android:name="de.appplant.cordova.plugin.localnotification.Restore"> <receiver android:name="de.appplant.cordova.plugin.localnotification.Restore">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.BOOT_COMPLETED" />
......
...@@ -12,6 +12,7 @@ module.exports = [ ...@@ -12,6 +12,7 @@ module.exports = [
"file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js", "file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js",
"id": "de.appplant.cordova.plugin.local-notification.LocalNotification", "id": "de.appplant.cordova.plugin.local-notification.LocalNotification",
"clobbers": [ "clobbers": [
"cordova.plugins.notification.local",
"plugin.notification.local" "plugin.notification.local"
] ]
}, },
......
...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) { ...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) {
exports.clear = function (ids, callback, scope) { exports.clear = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = Array.isArray(ids) ? ids : [ids];
ids = this.convertIds(ids); ids = this.convertIds(ids);
this.exec('clear', ids, callback, scope); this.exec('clear', ids, callback, scope);
}; };
...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {}; ...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) { exports.mergeWithDefaults = function (options) {
var defaults = this.getDefaults(); var defaults = this.getDefaults();
options.date = this.getValueFor(options, 'date', 'at', 'firstAt');
options.repeat = this.getValueFor(options, 'repeat', 'every');
options.message = this.getValueFor(options, 'message', 'text');
for (var key in defaults) { for (var key in defaults) {
if (options[key] === undefined) { if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key]; options[key] = defaults[key];
} }
} }
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options; return options;
}; };
...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) { ...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list * The converted property list
*/ */
exports.convertProperties = function (options) { exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) { options.id = options.id.toString();
options.date = new Date(); options.title = options.title.toString();
} options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.title) { if (isNaN(options.id)) {
options.title = options.title.toString(); options.id = this.getDefaults().id;
} }
if (options.message) { if (isNaN(options.badge)) {
options.message = options.message.toString(); options.badge = this.getDefaults().badge;
} }
if (options.text) { options.badge = Number(options.badge);
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
} }
if (typeof options.date == 'object') { if (typeof options.date == 'object') {
...@@ -613,6 +623,27 @@ exports.convertIds = function (ids) { ...@@ -613,6 +623,27 @@ exports.convertIds = function (ids) {
/** /**
* @private * @private
* *
* Return the first found value for the given keys.
*
* @param {Object} options
* Object with key-value properties
*
* @param {String[]} keys*
* Key list
*/
exports.getValueFor = function (options) {
var keys = Array.apply(null, arguments).slice(1);
for (var key in keys) {
if (options.hasOwnProperty(key)) {
return options[key];
}
}
};
/**
* @private
*
* Executes the native counterpart. * Executes the native counterpart.
* *
* @param {String} action * @param {String} action
......
...@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("add")) { if (action.equalsIgnoreCase("add")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray notifications = args.optJSONArray(0); add(args);
JSONObject arguments;
for(int i=0;i<notifications.length();i++){
arguments = notifications.optJSONObject(i);
LOG.d("LocalNotification", arguments.toString());
arguments = asset.parseURIs(arguments);
Options options = new Options(context).parse(arguments);
options.setInitDate();
nWrapper.schedule(options);
JSONArray data = new JSONArray().put(options.getJSONObject());
fireEvent("add", options.getId(), options.getJSON(), data);
}
command.success(); command.success();
} }
}); });
...@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("update")) { if (action.equalsIgnoreCase("update")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray updates = args.optJSONArray(0); update(args);
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
command.success(); command.success();
} }
}); });
...@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancel")) { if (action.equalsIgnoreCase("cancel")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray ids = args.optJSONArray(0); cancel(args);
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.cancel(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("cancel", id, "",data);
}
command.success(); command.success();
} }
...@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancelAll")) { if (action.equalsIgnoreCase("cancelAll")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray options = manager.getAll(); cancelAll(args);
nWrapper.cancelAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("cancel", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clear")) { if (action.equalsIgnoreCase("clear")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray ids = args.optJSONArray(0); clear(args);
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.clear(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("clear", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clearAll")) { if (action.equalsIgnoreCase("clearAll")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray options = manager.getAll(); clearAll(args);
nWrapper.clearAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("clear", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -274,7 +223,113 @@ public class LocalNotification extends CordovaPlugin { ...@@ -274,7 +223,113 @@ public class LocalNotification extends CordovaPlugin {
return true; return true;
} }
//------------------------------------------------exec-Functions-----------------------------------------------
/**
* Schedule notifications contained in the args-Array
* @param args
*/
private void add(JSONArray args){
JSONArray notifications = args;
JSONObject arguments;
for(int i=0;i<notifications.length();i++){
arguments = notifications.optJSONObject(i);
LOG.d("LocalNotification", arguments.toString());
arguments = asset.parseURIs(arguments);
Options options = new Options(context).parse(arguments);
options.setInitDate();
nWrapper.schedule(options);
JSONArray data = new JSONArray().put(options.getJSONObject());
fireEvent("add", options.getId(), options.getJSON(), data);
}
}
/**
* Update existing notifications
* @param args
*/
private void update(JSONArray args){
JSONArray updates = args;
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
}
/**
* Cancel scheduled Notifications
* @param args
*/
private void cancel(JSONArray args){
JSONArray ids = args;
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.cancel(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("cancel", id, "",data);
}
}
/**
* Cancel all scheduled notifications
* @param args
*/
public void cancelAll(JSONArray args){
JSONArray options = manager.getAll();
nWrapper.cancelAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("cancel", id, "",data);
}
}
/**
* Clear triggered notifications without cancel repeating.
* @param args
*/
public void clear(JSONArray args){
JSONArray ids = args;
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.clear(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("clear", id, "",data);
}
}
/**
* Clear all triggered notifications without cancel repeating.
* @param args
*/
public void clearAll(JSONArray args){
JSONArray options = manager.getAll();
nWrapper.clearAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("clear", id, "",data);
}
}
//-------------------------------------------------------------------------------------------------------------
/** /**
* Calls all pending callbacks after the deviceready event has been fired. * Calls all pending callbacks after the deviceready event has been fired.
*/ */
......
...@@ -32,6 +32,8 @@ import android.content.Context; ...@@ -32,6 +32,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import de.appplant.cordova.plugin.notification.*; import de.appplant.cordova.plugin.notification.*;
/** /**
...@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver { ...@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver {
if (options.getInterval() == 0) { if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId()); LocalNotification.unpersist(options.getId());
} }
builder.showNotificationToast(); nWrapper.showNotificationToast(options);
fireTriggerEvent(); fireTriggerEvent();
} else { } else {
builder.buildNotification(); Builder notification = builder.buildNotification();
builder.showNotification(); nWrapper.showNotification(notification, options);
} }
} }
......
...@@ -604,13 +604,16 @@ ...@@ -604,13 +604,16 @@
NSString* json = notification.options.json; NSString* json = notification.options.json;
NSString* args = [notification encodeToJSON]; NSString* args = [notification encodeToJSON];
json = [json stringByReplacingOccurrencesOfString:@"'"
withString:@"\\\\\\'"];
params = [NSString stringWithFormat: params = [NSString stringWithFormat:
@"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')", @"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')",
id, self.applicationState, json, args]; id, self.applicationState, json, args];
} }
js = [NSString stringWithFormat: js = [NSString stringWithFormat:
@"setTimeout('plugin.notification.local.on%@(%@)',0)", @"setTimeout('cordova.plugins.notification.local.on%@(%@)',0)",
event, params]; event, params];
if (deviceready) { if (deviceready) {
......
...@@ -12,6 +12,7 @@ module.exports = [ ...@@ -12,6 +12,7 @@ module.exports = [
"file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js", "file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js",
"id": "de.appplant.cordova.plugin.local-notification.LocalNotification", "id": "de.appplant.cordova.plugin.local-notification.LocalNotification",
"clobbers": [ "clobbers": [
"cordova.plugins.notification.local",
"plugin.notification.local" "plugin.notification.local"
] ]
}, },
......
...@@ -129,11 +129,7 @@ ...@@ -129,11 +129,7 @@
}; };
schedule = function () { schedule = function () {
plugin.notification.local.add({ window.plugin.notification.local.add({message: 'Great app!'});
id: id,
message: 'Test Message ' + (++counter),
json: { test:id }
});
}; };
scheduleMultiple = function () { scheduleMultiple = function () {
...@@ -229,18 +225,22 @@ ...@@ -229,18 +225,22 @@
<script type="text/javascript"> <script type="text/javascript">
document.addEventListener('deviceready', function () { document.addEventListener('deviceready', function () {
plugin.notification.local.onadd = function (id, state, json) { plugin.notification.local.onadd = function (id, state, json) {
console.log(arguments);
alert('on add\n' + Array.apply(null, arguments).join("\n")); alert('on add\n' + Array.apply(null, arguments).join("\n"));
}; };
plugin.notification.local.ontrigger = function (id, state, json) { plugin.notification.local.ontrigger = function (id, state, json) {
console.log(arguments);
alert('on trigger\n' + Array.apply(null, arguments).join("\n")); alert('on trigger\n' + Array.apply(null, arguments).join("\n"));
}; };
plugin.notification.local.onclick = function (id, state, json) { plugin.notification.local.onclick = function (id, state, json) {
console.log(arguments);
alert('on click\n' + Array.apply(null, arguments).join("\n")); alert('on click\n' + Array.apply(null, arguments).join("\n"));
}; };
plugin.notification.local.oncancel = function (id, state, json) { plugin.notification.local.oncancel = function (id, state, json) {
console.log(arguments);
alert('on cancel\n' + Array.apply(null, arguments).join("\n")); alert('on cancel\n' + Array.apply(null, arguments).join("\n"));
}; };
}, false); }, false);
......
...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) { ...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) {
exports.clear = function (ids, callback, scope) { exports.clear = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = Array.isArray(ids) ? ids : [ids];
ids = this.convertIds(ids); ids = this.convertIds(ids);
this.exec('clear', ids, callback, scope); this.exec('clear', ids, callback, scope);
}; };
...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {}; ...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) { exports.mergeWithDefaults = function (options) {
var defaults = this.getDefaults(); var defaults = this.getDefaults();
options.date = this.getValueFor(options, 'date', 'at', 'firstAt');
options.repeat = this.getValueFor(options, 'repeat', 'every');
options.message = this.getValueFor(options, 'message', 'text');
for (var key in defaults) { for (var key in defaults) {
if (options[key] === undefined) { if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key]; options[key] = defaults[key];
} }
} }
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options; return options;
}; };
...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) { ...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list * The converted property list
*/ */
exports.convertProperties = function (options) { exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) { options.id = options.id.toString();
options.date = new Date(); options.title = options.title.toString();
} options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.title) { if (isNaN(options.id)) {
options.title = options.title.toString(); options.id = this.getDefaults().id;
} }
if (options.message) { if (isNaN(options.badge)) {
options.message = options.message.toString(); options.badge = this.getDefaults().badge;
} }
if (options.text) { options.badge = Number(options.badge);
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
} }
if (typeof options.date == 'object') { if (typeof options.date == 'object') {
...@@ -613,6 +623,29 @@ exports.convertIds = function (ids) { ...@@ -613,6 +623,29 @@ exports.convertIds = function (ids) {
/** /**
* @private * @private
* *
* Return the first found value for the given keys.
*
* @param {Object} options
* Object with key-value properties
*
* @param {String[]} keys*
* Key list
*/
exports.getValueFor = function (options) {
var keys = Array.apply(null, arguments).slice(1);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
if (options.hasOwnProperty(key)) {
return options[key];
}
}
};
/**
* @private
*
* Executes the native counterpart. * Executes the native counterpart.
* *
* @param {String} action * @param {String} action
......
...@@ -35,11 +35,11 @@ ...@@ -35,11 +35,11 @@
"count": 1 "count": 1
}, },
{ {
"xml": "<receiver android:name=\"de.appplant.cordova.plugin.localnotification.Receiver\" />", "xml": "<receiver android:exported=\"false\" android:name=\"de.appplant.cordova.plugin.localnotification.Receiver\" />",
"count": 1 "count": 1
}, },
{ {
"xml": "<receiver android:name=\"de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver\" />", "xml": "<receiver android:exported=\"false\" android:name=\"de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver\" />",
"count": 1 "count": 1
}, },
{ {
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
<dependency id="org.apache.cordova.device" url="https://github.com/apache/cordova-plugin-device" /> <dependency id="org.apache.cordova.device" url="https://github.com/apache/cordova-plugin-device" />
<js-module src="www/local-notification.js" name="LocalNotification"> <js-module src="www/local-notification.js" name="LocalNotification">
<clobbers target="cordova.plugins.notification.local" />
<clobbers target="plugin.notification.local" /> <clobbers target="plugin.notification.local" />
</js-module> </js-module>
...@@ -64,13 +65,13 @@ ...@@ -64,13 +65,13 @@
* Android notification bar. The notification uses the default notification * Android notification bar. The notification uses the default notification
* sound and it vibrates the phone. * sound and it vibrates the phone.
--> -->
<receiver android:name="de.appplant.cordova.plugin.localnotification.Receiver" /> <receiver android:name="de.appplant.cordova.plugin.localnotification.Receiver" android:exported="false" />
<!-- <!--
* The delete intent receiver is triggered when the user clears a notification * The delete intent receiver is triggered when the user clears a notification
* manually. It unpersists the cleared notification from the shared preferences. * manually. It unpersists the cleared notification from the shared preferences.
--> -->
<receiver android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" /> <receiver android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" android:exported="false"/>
<!-- <!--
* This class is triggered upon reboot of the device. It needs to re-register * This class is triggered upon reboot of the device. It needs to re-register
......
...@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("add")) { if (action.equalsIgnoreCase("add")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray notifications = args.optJSONArray(0); add(args);
JSONObject arguments;
for(int i=0;i<notifications.length();i++){
arguments = notifications.optJSONObject(i);
LOG.d("LocalNotification", arguments.toString());
arguments = asset.parseURIs(arguments);
Options options = new Options(context).parse(arguments);
options.setInitDate();
nWrapper.schedule(options);
JSONArray data = new JSONArray().put(options.getJSONObject());
fireEvent("add", options.getId(), options.getJSON(), data);
}
command.success(); command.success();
} }
}); });
...@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("update")) { if (action.equalsIgnoreCase("update")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray updates = args.optJSONArray(0); update(args);
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
command.success(); command.success();
} }
}); });
...@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancel")) { if (action.equalsIgnoreCase("cancel")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray ids = args.optJSONArray(0); cancel(args);
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.cancel(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("cancel", id, "",data);
}
command.success(); command.success();
} }
...@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancelAll")) { if (action.equalsIgnoreCase("cancelAll")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray options = manager.getAll(); cancelAll(args);
nWrapper.cancelAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("cancel", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clear")) { if (action.equalsIgnoreCase("clear")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray ids = args.optJSONArray(0); clear(args);
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.clear(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("clear", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clearAll")) { if (action.equalsIgnoreCase("clearAll")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONArray options = manager.getAll(); clearAll(args);
nWrapper.clearAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("clear", id, "",data);
}
command.success(); command.success();
} }
}); });
...@@ -274,7 +223,113 @@ public class LocalNotification extends CordovaPlugin { ...@@ -274,7 +223,113 @@ public class LocalNotification extends CordovaPlugin {
return true; return true;
} }
//------------------------------------------------exec-Functions-----------------------------------------------
/**
* Schedule notifications contained in the args-Array
* @param args
*/
private void add(JSONArray args){
JSONArray notifications = args;
JSONObject arguments;
for(int i=0;i<notifications.length();i++){
arguments = notifications.optJSONObject(i);
LOG.d("LocalNotification", arguments.toString());
arguments = asset.parseURIs(arguments);
Options options = new Options(context).parse(arguments);
options.setInitDate();
nWrapper.schedule(options);
JSONArray data = new JSONArray().put(options.getJSONObject());
fireEvent("add", options.getId(), options.getJSON(), data);
}
}
/**
* Update existing notifications
* @param args
*/
private void update(JSONArray args){
JSONArray updates = args;
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
}
/**
* Cancel scheduled Notifications
* @param args
*/
private void cancel(JSONArray args){
JSONArray ids = args;
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.cancel(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("cancel", id, "",data);
}
}
/**
* Cancel all scheduled notifications
* @param args
*/
public void cancelAll(JSONArray args){
JSONArray options = manager.getAll();
nWrapper.cancelAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("cancel", id, "",data);
}
}
/**
* Clear triggered notifications without cancel repeating.
* @param args
*/
public void clear(JSONArray args){
JSONArray ids = args;
String id;
for(int i=0;i<ids.length();i++){
id = args.optString(i);
nWrapper.clear(id);
JSONArray managerId = new JSONArray().put(id);
JSONArray data = new JSONArray().put(manager.getAll(managerId));
fireEvent("clear", id, "",data);
}
}
/**
* Clear all triggered notifications without cancel repeating.
* @param args
*/
public void clearAll(JSONArray args){
JSONArray options = manager.getAll();
nWrapper.clearAll();
String id;
JSONObject arguments;
for(int i=0;i<options.length();i++){
arguments= (JSONObject) options.opt(i);
JSONArray data = new JSONArray().put(arguments);
id = arguments.optString("id");
fireEvent("clear", id, "",data);
}
}
//-------------------------------------------------------------------------------------------------------------
/** /**
* Calls all pending callbacks after the deviceready event has been fired. * Calls all pending callbacks after the deviceready event has been fired.
*/ */
......
...@@ -32,6 +32,8 @@ import android.content.Context; ...@@ -32,6 +32,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import de.appplant.cordova.plugin.notification.*; import de.appplant.cordova.plugin.notification.*;
/** /**
...@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver { ...@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver {
if (options.getInterval() == 0) { if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId()); LocalNotification.unpersist(options.getId());
} }
builder.showNotificationToast(); nWrapper.showNotificationToast(options);
fireTriggerEvent(); fireTriggerEvent();
} else { } else {
builder.buildNotification(); Builder notification = builder.buildNotification();
builder.showNotification(); nWrapper.showNotification(notification, options);
} }
} }
......
...@@ -604,6 +604,9 @@ ...@@ -604,6 +604,9 @@
NSString* json = notification.options.json; NSString* json = notification.options.json;
NSString* args = [notification encodeToJSON]; NSString* args = [notification encodeToJSON];
json = [json stringByReplacingOccurrencesOfString:@"'"
withString:@"\\\\\\'"];
params = [NSString stringWithFormat: params = [NSString stringWithFormat:
@"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')", @"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')",
id, self.applicationState, json, args]; id, self.applicationState, json, args];
......
...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) { ...@@ -159,7 +159,7 @@ exports.update = function (opts, callback, scope) {
exports.clear = function (ids, callback, scope) { exports.clear = function (ids, callback, scope) {
ids = Array.isArray(ids) ? ids : [ids]; ids = Array.isArray(ids) ? ids : [ids];
ids = this.convertIds(ids); ids = this.convertIds(ids);
this.exec('clear', ids, callback, scope); this.exec('clear', ids, callback, scope);
}; };
...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {}; ...@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) { exports.mergeWithDefaults = function (options) {
var defaults = this.getDefaults(); var defaults = this.getDefaults();
options.date = this.getValueFor(options, 'date', 'at', 'firstAt');
options.repeat = this.getValueFor(options, 'repeat', 'every');
options.message = this.getValueFor(options, 'message', 'text');
for (var key in defaults) { for (var key in defaults) {
if (options[key] === undefined) { if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key]; options[key] = defaults[key];
} }
} }
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options; return options;
}; };
...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) { ...@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list * The converted property list
*/ */
exports.convertProperties = function (options) { exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
if (options.date === undefined) { options.id = options.id.toString();
options.date = new Date(); options.title = options.title.toString();
} options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.title) { if (isNaN(options.id)) {
options.title = options.title.toString(); options.id = this.getDefaults().id;
} }
if (options.message) { if (isNaN(options.badge)) {
options.message = options.message.toString(); options.badge = this.getDefaults().badge;
} }
if (options.text) { options.badge = Number(options.badge);
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
} }
if (typeof options.date == 'object') { if (typeof options.date == 'object') {
...@@ -613,6 +623,27 @@ exports.convertIds = function (ids) { ...@@ -613,6 +623,27 @@ exports.convertIds = function (ids) {
/** /**
* @private * @private
* *
* Return the first found value for the given keys.
*
* @param {Object} options
* Object with key-value properties
*
* @param {String[]} keys*
* Key list
*/
exports.getValueFor = function (options) {
var keys = Array.apply(null, arguments).slice(1);
for (var key in keys) {
if (options.hasOwnProperty(key)) {
return options[key];
}
}
};
/**
* @private
*
* Executes the native counterpart. * Executes the native counterpart.
* *
* @param {String} action * @param {String} action
......
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