Commit c76410b6 authored by PKnittel's avatar PKnittel
Browse files

Added Content from Enhancements and most Features from 0.8.x. TODO-List on android

Added:
Ability to schedule, clear and cancel multiple notifications at once
Ability to update existing notifications
Clear methods on JavaScript-side
parent e38a8384
Loading
Loading
Loading
Loading
+126 −0
Changes for src/android/LocalNotification.java: 126 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -75,8 +75,64 @@ public class LocalNotification extends CordovaPlugin {
                public void run() {               	
                    JSONObject arguments = setInitDate(args.optJSONObject(0));
                    Options options      = new Options(context).parse(arguments);
                    add(options, true);
                    command.success();
                }
            });
        }
        
        if (action.equalsIgnoreCase("addMultiple")) {
            cordova.getThreadPool().execute( new Runnable() {
                public void run() {    
                	JSONArray notifications = args.optJSONArray(0);
                	for (int i =0; i<notifications.length();i++){
                		JSONObject arguments = setInitDate(notifications.optJSONObject(i));
                		Options options      = new Options(context).parse(arguments);
                		add(options, true);
                	}
                    command.success();
                }
            });
        }
        
        if (action.equalsIgnoreCase("update")) {
        	cordova.getThreadPool().execute( new Runnable() {
                public void run() {
                	JSONObject updates = args.optJSONObject(0);
                	
                	update(updates);
                	command.success();
                }
            });
        }
        
        if (action.equalsIgnoreCase("clear")) {
        	cordova.getThreadPool().execute( new Runnable() {
                public void run() {
                	String id = args.optString(0);

                    clear(id);
                    command.success();
                }
            });
        }
        
        if (action.equalsIgnoreCase("clearMultiple")) {
        	cordova.getThreadPool().execute( new Runnable() {
                public void run() {
                	JSONArray ids = args.optJSONArray(0);
                	for (int i =0; i<ids.length();i++){
                        clear(ids.optString(i));
                	}
                    command.success();
                }
            });
        }
        
        if (action.equalsIgnoreCase("clearAll")) {
        	cordova.getThreadPool().execute( new Runnable() {
                public void run() {
                	clearAll();
                    command.success();
                }
            });
@@ -94,6 +150,18 @@ public class LocalNotification extends CordovaPlugin {
            });
        }
        
        if (action.equalsIgnoreCase("cancelMultiple")) {
        	cordova.getThreadPool().execute( new Runnable() {
                public void run() {
                	JSONArray ids = args.optJSONArray(0);
                	for (int i =0; i<ids.length();i++){
                        cancel(ids.optString(i));
                	}
                    command.success();
                }
            });
        }

        if (action.equalsIgnoreCase("cancelAll")) {
            cordova.getThreadPool().execute( new Runnable() {
                public void run() {
@@ -184,6 +252,38 @@ public class LocalNotification extends CordovaPlugin {
        am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
    }

    /** 
     * Update an existing notification 
     * 
     * @param updates JSONObject with update-content
     */
    public static void update (JSONObject updates){
    	String id = updates.optString("id", "0");
    	
    	// update shared preferences
    	SharedPreferences settings = getSharedPreferences();
    	Map<String, ?> alarms      = settings.getAll();
    	JSONObject arguments;
		try {
			arguments = new JSONObject(alarms.get(id).toString());
		} catch (JSONException e) {
			e.printStackTrace();
			return;
		}
		arguments = updateArguments(arguments, updates);
		    	
    	// cancel existing alarm
        Intent intent = new Intent(context, Receiver.class)
        	.setAction("" + id);
        PendingIntent pi       = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager am        = getAlarmManager();
        am.cancel(pi);
        
        //add new alarm
        Options options      = new Options(context).parse(arguments);
        add(options,false);        
    }
    
    /**
     * Clear a specific notification without canceling repeating alarms
     * 
@@ -213,6 +313,7 @@ public class LocalNotification extends CordovaPlugin {
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return;
		}
		
        fireEvent("clear", notificationId, "");
@@ -220,6 +321,7 @@ public class LocalNotification extends CordovaPlugin {
    
    /**
     * Clear all notifications without canceling repeating alarms
     * 
     */
    public static void clearAll (){
        SharedPreferences settings = getSharedPreferences();
@@ -553,5 +655,29 @@ public class LocalNotification extends CordovaPlugin {
    	return arguments;
    }
    
    private static JSONObject updateArguments(JSONObject arguments,JSONObject updates){
    	try	{
    		if(!updates.isNull("message")){
    			arguments.put("message", updates.get("message"));
    		}
    		if(!updates.isNull("title")){
    			arguments.put("title", updates.get("title"));
    		}
    		if(!updates.isNull("badge")){
    			arguments.put("badge", updates.get("badge"));
    		}
    		if(!updates.isNull("sound")){
    			arguments.put("sound", updates.get("sound"));
    		}
    		if(!updates.isNull("icon")){
    			arguments.put("icon", updates.get("icon"));
    		}
    	} catch (JSONException jse){
    		jse.printStackTrace();
    	}
    	
    	return arguments;
    }
    
  
}
+156 −0
Changes for www/local-notification.js: 156 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -147,6 +147,128 @@ exports.add = function (props, callback, scope) {
    return options.id;
};

/**
 * Add new entries to the registry (more than one)
 *
 * @param {Object} options
 *      The notification properties
 * @param {Function} callback
 *      A function to be called after the notification has been added
 * @param {Object} scope
 *      The scope for the callback function
 *
 * @return {Number}
 *      The notification's ID
 */
exports.addMultiple = function (notifications, callback, scope) {
	var length = notifications.length;
	var notificationsMerged = new Array(),
		callbackFn = this.createCallbackFn(callback, scope);
	for (var i=0;i<length;i++){
		var options    = this.mergeWithDefaults(notifications[i]);
		if (options.id) {
			options.id = options.id.toString();
		}

		if (options.date === undefined) {
			options.date = new Date();
		}

		if (options.title) {
			options.title = options.title.toString();
		}

		if (options.message) {
			options.message = options.message.toString();
		}

		if (typeof options.date == 'object') {
			options.date = Math.round(options.date.getTime()/1000);
		}

		if (['WinCE', 'Win32NT'].indexOf(device.platform) > -1) {
			callbackFn = function (cmd) {
				eval(cmd);
			};
		}
		notificationsMerged.push(options);
	}

    cordova.exec(callbackFn, null, 'LocalNotification', 'addMultiple', notificationsMerged);

    return options.id;
};

/**
 * Update existing notification (currently android only)
 *
 * @param {Object} options
 *      The notification properties to update
 * @param {Function} callback
 *      A function to be called after the notification has been updated
 * @param {Object} scope
 *      The scope for the callback function
 *
 * @return {Number}
 *      The notification's ID
 */
exports.update = function (updates, callback, scope) {
	callbackFn = this.createCallbackFn(callback, scope);
	cordova.exec(callbackFn, null, 'LocalNotification', 'update', [updates]);
};

/**
 * Clears the specified notification.
 *
 * @param {String} id
 *      The ID of the notification
 * @param {Function} callback
 *      A function to be called after the notification has been cleared
 * @param {Object} scope
 *      The scope for the callback function
 */
exports.clear = function (id, callback, scope) {
	var id         = id.toString(),
		callbackFn = this.createCallbackFn(callback, scope);
	cordova.exec(callbackFn, null, 'LocalNotification', 'clear', [id]);
};
	
/**
 * Clear the specified notifications (more than one).
 *
 * @param {String} id
 *      The ID of the notification
 * @param {Function} callback
 *      A function to be called after the notifications has been cleared.
 * @param {Object} scope
 *      The scope for the callback function
 */
exports.clearMultiple = function (ids, callback, scope) {
	var length = ids.length;
	var idArray = new Array(),
		callbackFn = this.createCallbackFn(callback, scope);
	for (var i=0;i<length;i++){
		var id         = ids[i].toString();
		idArray.push(id);	
	}
	var callbackFn = this.createCallbackFn(callback, scope);
	cordova.exec(callbackFn, null, 'LocalNotification', 'clearMultiple', [ids]);
};
	
/**
 * Clears all previously sheduled notifications.
 *
 * @param {Function} callback
 *      A function to be called after all notifications have been cleared
 * @param {Object} scope
 *      The scope for the callback function
 */
exports.clearAll = function (callback, scope) {
        var callbackFn = this.createCallbackFn(callback, scope);

        cordova.exec(callbackFn, null, 'LocalNotification', 'clearAll', []);
};

/**
 * Cancels the specified notification.
 *
@@ -163,6 +285,28 @@ exports.cancel = function (id, callback, scope) {
    exec(fn, null, 'LocalNotification', 'cancel', [(id || '0').toString()]);
};

/**
 * Cancel the specified notifications (more than one).
 *
 * @param {String} id
 *      The ID of the notification
 * @param {Function} callback
 *      A function to be called after the notifications has been canceled
 * @param {Object} scope
 *      The scope for the callback function
 */
exports.cancelMultiple = function (ids, callback, scope) {
	var length = ids.length;
	var idArray = new Array(),
		callbackFn = this.createCallbackFn(callback, scope);
	for (var i=0;i<length;i++){
		var id         = ids[i].toString();
		idArray.push(id);	
	}
	var callbackFn = this.createCallbackFn(callback, scope);
	cordova.exec(callbackFn, null, 'LocalNotification', 'cancelMultiple', [ids]);
};

/**
 * Removes all previously registered notifications.
 *
@@ -328,6 +472,18 @@ exports.onclick = function (id, state, json) {};
 */
exports.oncancel = function (id, state, json) {};

/**
 * Get fired when the notification was cleared.
 *
 * @param {String} id
 *      The ID of the notification
 * @param {String} state
 *      Either "foreground" or "background"
 * @param {String} json
 *      A custom (JSON) string
 */
exports.onclear = function (id, state, json) {};


/**
 * @private