| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /*
- * 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 = [], actions = [];
- for (var i = 0, props, opts; i < args.length; i++) {
- props = args[i];
- opts = new LocalNotification.Options();
- for (var prop in opts) {
- if (prop != 'actions' && props[prop]) opts[prop] = props[prop];
- }
- for (var j = 0, action, btn; j < props.actions.length; j++) {
- action = props.actions[j];
- if (!action.type || action.type == 'button') {
- btn = new LocalNotification.Button();
- } else
- if (action.type == 'input') {
- btn = new LocalNotification.Input();
- }
- for (prop in btn) {
- if (action[prop]) btn[prop] = action[prop];
- }
- actions.push(btn);
- }
- opts.actions = actions;
- options.push(opts);
- }
- impl.schedule(options);
- success();
- };
- /**
- * 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(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(Array.from(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(Array.from(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(Array.from(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',
- e = { event: event };
- if (input && input.size > 0) {
- var it = input.first();
- e.text = it.current.value;
- while (it.hasCurrent) {
- e[it.current.key] = it.current.value;
- it.moveNext();
- }
- }
- cordova.plugins.notification.local.core.fireEvent(event, toast, e);
- };
- // 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);
|