| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- /*
- * Apache 2.0 License
- *
- * Copyright (c) Sebastian Katzer 2017
- *
- * This file contains Original Code and/or Modifications of Original Code
- * as defined in and that are subject to the Apache License
- * Version 2.0 (the 'License'). You may not use this file except in
- * compliance with the License. Please obtain a copy of the License at
- * http://opensource.org/licenses/Apache-2.0/ and read it before using this
- * file.
- *
- * The Original Code and all software distributed under the License are
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
- * Please see the License for the specific language governing rights and
- * limitations under the License.
- */
- var LocalNotification = LocalNotificationProxy.LocalNotification,
- ActivationKind = Windows.ApplicationModel.Activation.ActivationKind;
- var impl = new LocalNotificationProxy.LocalNotificationProxy();
- /**
- * Check permission to show notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.check = function (success, error) {
- var granted = impl.hasPermission();
- success(granted);
- };
- /**
- * Request permission to show notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.request = function (success, error) {
- exports.check(success, error);
- };
- /**
- * Schedule notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.schedule = function (success, error, args) {
- var options = [];
- for (var i = 0, props, opts; i < args.length; i++) {
- props = args[i];
- opts = exports.parseOptions(props);
- options.push(opts);
- }
- impl.schedule(options);
- for (i = 0; i < options.length; i++) {
- exports.fireEvent('add', options[i]);
- }
- success();
- };
- /**
- * Update notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.schedule = function (success, error, args) {
- console.warn('LocalNotification#update is not implemented.');
- success();
- };
- /**
- * Clear the notifications specified by id.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.clear = function (success, error, args) {
- var toasts = impl.clear(args) || [];
- for (var i = 0; i < toasts.length; i++) {
- exports.fireEvent('clear', toasts[i]);
- }
- success();
- };
- /**
- * Clear all notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.clearAll = function (success, error) {
- impl.clearAll();
- exports.fireEvent('clearall');
- success();
- };
- /**
- * Cancel the notifications specified by id.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.cancel = function (success, error, args) {
- var toasts = impl.cancel(args) || [];
- for (var i = 0; i < toasts.length; i++) {
- exports.fireEvent('cancel', toasts[i]);
- }
- success();
- };
- /**
- * Cancel all notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.cancelAll = function (success, error) {
- impl.cancelAll();
- exports.fireEvent('cancelall');
- success();
- };
- /**
- * Get the type of notification.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.type = function (success, error, args) {
- var type = impl.type(args[0]);
- success(type);
- };
- /**
- * List of all notification ids.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.ids = function (success, error) {
- var ids = impl.ids() || [];
- success(Array.from(ids));
- };
- /**
- * List of all scheduled notification ids.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.scheduledIds = function (success, error) {
- var ids = impl.scheduledIds() || [];
- success(Array.from(ids));
- };
- /**
- * List of all triggered notification ids.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.triggeredIds = function (success, error) {
- var ids = impl.triggeredIds() || [];
- success(Array.from(ids));
- };
- /**
- * Get a single notification by id.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.notification = function (success, error, args) {
- var obj = impl.notification(args[0]);
- success(exports.clone(obj));
- };
- /**
- * List of (all) notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- * @param [ Array ] args Interface arguments
- *
- * @return [ Void ]
- */
- exports.notifications = function (success, error, args) {
- var objs = impl.notifications(args) || [];
- success(exports.cloneAll(objs));
- };
- /**
- * List of all scheduled notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.scheduledNotifications = function (success, error) {
- var objs = impl.scheduledNotifications() || [];
- success(exports.cloneAll(objs));
- };
- /**
- * List of all triggered notifications.
- *
- * @param [ Function ] success Success callback
- * @param [ Function ] error Error callback
- *
- * @return [ Void ]
- */
- exports.triggeredNotifications = function (success, error) {
- var objs = impl.triggeredNotifications() || [];
- success(exports.cloneAll(objs));
- };
- /**
- * Inform the user through the click event that a notification was clicked.
- *
- * @param [ String ] xml The launch identifier.
- *
- * @return [ Void ]
- */
- exports.clicked = function (xml, input) {
- var toast = LocalNotification.Options.parse(xml),
- event = toast.action || 'click',
- meta = Object.assign({}, input);
- if (input && input.size > 0) {
- meta.text = input.first().current.value;
- }
- exports.fireEvent(event, toast, meta);
- };
- /**
- * Invoke listeners for the given event.
- *
- * @param [ String ] event The name of the event.
- * @param [ Object ] toast Optional notification object.
- * @param [ Object ] data Optional meta data about the event.
- *
- * @return [ Void ]
- */
- exports.fireEvent = function (event, toast, data) {
- var meta = Object.assign({ event: event }, data),
- plugin = cordova.plugins.notification.local.core;
- if (toast) {
- plugin.fireEvent(event, exports.clone(toast), meta);
- } else {
- plugin.fireEvent(event, meta);
- }
- };
- /**
- * Clone the objects and delete internal properties.
- *
- * @param [ Array<Object> ] objs The objects to clone for.
- *
- * @return [ Array<Object> ]
- */
- exports.cloneAll = function (objs) {
- var clones = [];
- for (var i = 0; i < objs.length; i++) {
- clones.push(exports.clone(objs[i]));
- }
- return clones;
- };
- /**
- * Clone the object and delete internal properties.
- *
- * @param [ Object ] obj The object to clone for.
- *
- * @return [ Object ]
- */
- exports.clone = function (obj) {
- var ignore = ['action'],
- clone = {};
- for (var prop in obj) {
- if (ignore.includes(prop) || typeof obj[prop] === 'function')
- continue;
- try {
- clone[prop] = obj[prop];
- } catch (e) {
- clone[prop] = null;
- }
- }
- return clone;
- };
- /**
- * Parse notification spec into an instance of prefered type.
- *
- * @param [ Object ] obj The notification options map.
- *
- * @return [ LocalNotification.Options ]
- */
- exports.parseOptions = function (obj) {
- var opts = new LocalNotification.Options(),
- ignore = ['actions', 'trigger'];
- for (var prop in opts) {
- if (!ignore.includes(prop) && obj[prop]) {
- opts[prop] = obj[prop];
- }
- }
- var trigger = exports.parseTrigger(obj);
- opts.trigger = trigger;
- var actions = exports.parseActions(obj);
- opts.actions = actions;
- return opts;
- };
- /**
- * Parse trigger spec into instance of prefered type.
- *
- * @param [ Object ] obj The notification options map.
- *
- * @return [ LocalNotification.Trigger ]
- */
- exports.parseTrigger = function (obj) {
- var trigger = new LocalNotification.Trigger(),
- spec = obj.trigger, val;
- if (!spec) return trigger;
- for (var prop in trigger) {
- val = spec[prop];
- if (!val) continue;
- trigger[prop] = prop == 'every' ? val.toString() : val;
- }
- return trigger;
- };
- /**
- * Parse action specs into instances of prefered types.
- *
- * @param [ Object ] obj The notification options map.
- *
- * @return [ Array<LocalNotification.Action> ]
- */
- exports.parseActions = function (obj) {
- var actions = [];
- if (!obj.actions) return actions;
- for (var i = 0, action, btn; i < obj.actions.length; i++) {
- action = obj.actions[i];
- if (!action.type || action.type == 'button') {
- btn = new LocalNotification.Button();
- } else
- if (action.type == 'input') {
- btn = new LocalNotification.Input();
- }
- for (var prop in btn) {
- if (action[prop]) btn[prop] = action[prop];
- }
- actions.push(btn);
- }
- return actions;
- };
- // Handle onclick event
- document.addEventListener('activated', function (e) {
- if (e.kind == ActivationKind.toastNotification) {
- exports.clicked(e.raw.argument, e.raw.userInput);
- }
- }, false);
- cordova.commandProxy.add('LocalNotification', exports);
|