Commit 01ffae10 by Sebastián Katzer

Update example

parent aa24cd44
......@@ -10,8 +10,8 @@
</intent-filter>
</activity>
<service android:name="de.appplant.cordova.plugin.background.ForegroundService" />
<receiver 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.Receiver" />
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" />
<receiver android:name="de.appplant.cordova.plugin.localnotification.Restore">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
......
......@@ -12,6 +12,7 @@ module.exports = [
"file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js",
"id": "de.appplant.cordova.plugin.local-notification.LocalNotification",
"clobbers": [
"cordova.plugins.notification.local",
"plugin.notification.local"
]
},
......
......@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) {
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) {
if (options[key] === undefined) {
if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key];
}
}
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options;
};
......@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list
*/
exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
options.title = options.title.toString();
options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.date === undefined) {
options.date = new Date();
if (isNaN(options.id)) {
options.id = this.getDefaults().id;
}
if (options.title) {
options.title = options.title.toString();
if (isNaN(options.badge)) {
options.badge = this.getDefaults().badge;
}
if (options.message) {
options.message = options.message.toString();
}
options.badge = Number(options.badge);
if (options.text) {
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
}
if (typeof options.date == 'object') {
......@@ -613,6 +623,27 @@ exports.convertIds = function (ids) {
/**
* @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.
*
* @param {String} action
......
......@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("add")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray notifications = args.optJSONArray(0);
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);
}
add(args);
command.success();
}
});
......@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("update")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray updates = args.optJSONArray(0);
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
update(args);
command.success();
}
});
......@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancel")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray ids = args.optJSONArray(0);
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(args);
command.success();
}
......@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancelAll")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
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);
}
cancelAll(args);
command.success();
}
});
......@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clear")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray ids = args.optJSONArray(0);
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(args);
command.success();
}
});
......@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clearAll")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
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);
}
clearAll(args);
command.success();
}
});
......@@ -275,6 +224,112 @@ public class LocalNotification extends CordovaPlugin {
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.
*/
......
......@@ -32,6 +32,8 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import de.appplant.cordova.plugin.notification.*;
/**
......@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver {
if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId());
}
builder.showNotificationToast();
nWrapper.showNotificationToast(options);
fireTriggerEvent();
} else {
builder.buildNotification();
Builder notification = builder.buildNotification();
builder.showNotification();
nWrapper.showNotification(notification, options);
}
}
......
......@@ -604,13 +604,16 @@
NSString* json = notification.options.json;
NSString* args = [notification encodeToJSON];
json = [json stringByReplacingOccurrencesOfString:@"'"
withString:@"\\\\\\'"];
params = [NSString stringWithFormat:
@"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')",
id, self.applicationState, json, args];
}
js = [NSString stringWithFormat:
@"setTimeout('plugin.notification.local.on%@(%@)',0)",
@"setTimeout('cordova.plugins.notification.local.on%@(%@)',0)",
event, params];
if (deviceready) {
......
......@@ -12,6 +12,7 @@ module.exports = [
"file": "plugins/de.appplant.cordova.plugin.local-notification/www/local-notification.js",
"id": "de.appplant.cordova.plugin.local-notification.LocalNotification",
"clobbers": [
"cordova.plugins.notification.local",
"plugin.notification.local"
]
},
......
......@@ -129,11 +129,7 @@
};
schedule = function () {
plugin.notification.local.add({
id: id,
message: 'Test Message ' + (++counter),
json: { test:id }
});
window.plugin.notification.local.add({message: 'Great app!'});
};
scheduleMultiple = function () {
......@@ -229,18 +225,22 @@
<script type="text/javascript">
document.addEventListener('deviceready', function () {
plugin.notification.local.onadd = function (id, state, json) {
console.log(arguments);
alert('on add\n' + Array.apply(null, arguments).join("\n"));
};
plugin.notification.local.ontrigger = function (id, state, json) {
console.log(arguments);
alert('on trigger\n' + Array.apply(null, arguments).join("\n"));
};
plugin.notification.local.onclick = function (id, state, json) {
console.log(arguments);
alert('on click\n' + Array.apply(null, arguments).join("\n"));
};
plugin.notification.local.oncancel = function (id, state, json) {
console.log(arguments);
alert('on cancel\n' + Array.apply(null, arguments).join("\n"));
};
}, false);
......
......@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) {
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) {
if (options[key] === undefined) {
if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key];
}
}
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options;
};
......@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list
*/
exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
options.title = options.title.toString();
options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.date === undefined) {
options.date = new Date();
if (isNaN(options.id)) {
options.id = this.getDefaults().id;
}
if (options.title) {
options.title = options.title.toString();
if (isNaN(options.badge)) {
options.badge = this.getDefaults().badge;
}
if (options.message) {
options.message = options.message.toString();
}
options.badge = Number(options.badge);
if (options.text) {
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
}
if (typeof options.date == 'object') {
......@@ -613,6 +623,29 @@ exports.convertIds = function (ids) {
/**
* @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.
*
* @param {String} action
......
......@@ -35,11 +35,11 @@
"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
},
{
"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
},
{
......
......@@ -21,6 +21,7 @@
<dependency id="org.apache.cordova.device" url="https://github.com/apache/cordova-plugin-device" />
<js-module src="www/local-notification.js" name="LocalNotification">
<clobbers target="cordova.plugins.notification.local" />
<clobbers target="plugin.notification.local" />
</js-module>
......@@ -64,13 +65,13 @@
* Android notification bar. The notification uses the default notification
* 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
* 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
......
......@@ -81,18 +81,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("add")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray notifications = args.optJSONArray(0);
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);
}
add(args);
command.success();
}
});
......@@ -101,13 +90,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("update")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray updates = args.optJSONArray(0);
JSONObject updateContent;
for(int i=0;i<updates.length();i++){
updateContent = args.optJSONObject(i);
nWrapper.update(updateContent);
}
update(args);
command.success();
}
});
......@@ -116,15 +99,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancel")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray ids = args.optJSONArray(0);
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(args);
command.success();
}
......@@ -134,16 +109,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("cancelAll")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
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);
}
cancelAll(args);
command.success();
}
});
......@@ -152,15 +118,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clear")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
JSONArray ids = args.optJSONArray(0);
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(args);
command.success();
}
});
......@@ -169,16 +127,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("clearAll")) {
cordova.getThreadPool().execute( new Runnable() {
public void run() {
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);
}
clearAll(args);
command.success();
}
});
......@@ -275,6 +224,112 @@ public class LocalNotification extends CordovaPlugin {
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.
*/
......
......@@ -32,6 +32,8 @@ import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import de.appplant.cordova.plugin.notification.*;
/**
......@@ -80,12 +82,12 @@ public class Receiver extends BroadcastReceiver {
if (options.getInterval() == 0) {
LocalNotification.unpersist(options.getId());
}
builder.showNotificationToast();
nWrapper.showNotificationToast(options);
fireTriggerEvent();
} else {
builder.buildNotification();
Builder notification = builder.buildNotification();
builder.showNotification();
nWrapper.showNotification(notification, options);
}
}
......
......@@ -604,6 +604,9 @@
NSString* json = notification.options.json;
NSString* args = [notification encodeToJSON];
json = [json stringByReplacingOccurrencesOfString:@"'"
withString:@"\\\\\\'"];
params = [NSString stringWithFormat:
@"\"%@\",\"%@\",\\'%@\\',JSON.parse(\\'%@\\')",
id, self.applicationState, json, args];
......
......@@ -488,12 +488,22 @@ exports.onclear = function (id, state, json, data) {};
exports.mergeWithDefaults = function (options) {
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) {
if (options[key] === undefined) {
if (options[key] === null || options[key] === undefined) {
options[key] = defaults[key];
}
}
for (key in options) {
if (!defaults.hasOwnProperty(key)) {
delete options[key];
}
}
return options;
};
......@@ -509,24 +519,24 @@ exports.mergeWithDefaults = function (options) {
* The converted property list
*/
exports.convertProperties = function (options) {
if (options.id) {
options.id = options.id.toString();
}
options.title = options.title.toString();
options.message = options.message.toString();
options.autoCancel = options.autoCancel === true;
if (options.date === undefined) {
options.date = new Date();
if (isNaN(options.id)) {
options.id = this.getDefaults().id;
}
if (options.title) {
options.title = options.title.toString();
if (isNaN(options.badge)) {
options.badge = this.getDefaults().badge;
}
if (options.message) {
options.message = options.message.toString();
}
options.badge = Number(options.badge);
if (options.text) {
options.message = options.text.toString();
if (options.date === undefined || options.date === null) {
options.date = new Date();
}
if (typeof options.date == 'object') {
......@@ -613,6 +623,27 @@ exports.convertIds = function (ids) {
/**
* @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.
*
* @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