| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426 |
- /*
- Copyright 2013-2015 appPlant UG
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
- */
- var Notifications = Windows.UI.Notifications,
- applicationData = Windows.Storage.ApplicationData.current,
- localSettings = applicationData.localSettings;
- exports.core = {
- /**
- * Executes all queued events.
- */
- deviceready: function () {
- var plugin = cordova.plugins.notification.local,
- events = this.eventQueue;
- this.isReady = true;
- for (var i = 0; i < events.length; i++) {
- plugin.fireEvent.apply(plugin, events[i]);
- }
- this.eventQueue = [];
- },
- /**
- * Schedules new local notifications.
- *
- * @param {Object[]} notifications
- * Array of local notifications
- * @param {String} event
- * 'schedule' or 'update'
- */
- schedule: function (notifications) {
- var triggerFn = function (notification) {
- this.updateBadge(notification.badge);
- this.fireEvent('trigger', notification);
- };
- for (var i = 0; i < notifications.length; i++) {
- var options = notifications[i],
- notification = this.build(options);
- this.cancelLocalNotification(options.id);
- this.scheduleLocalNotification(notification, options);
- this.scheduleBackupNotification(notification, options);
- this.fireEvent('schedule', options);
- this.callOnTrigger(options, triggerFn);
- }
- },
- /**
- * Schedules a single local notification.
- *
- * @param {Windows.Data.Xml.Dom.XmlDocument} notification
- * The local notification
- * @param {Object} options
- * Local notification properties
- */
- scheduleLocalNotification: function (notification, options) {
- var interval = this.getRepeatInterval(options.every),
- triggerTime = new Date((options.at * 1000)),
- now = new Date().getTime(),
- toast;
- if (triggerTime <= now) {
- triggerTime = new Date(now + 10);
- }
- try {
- if (interval !== 0 && interval < 360001 && interval > 59999) {
- toast = new Notifications.ScheduledToastNotification(
- notification, triggerTime, interval, 5);
- } else {
- toast = new Notifications.ScheduledToastNotification(
- notification, triggerTime);
- }
- } catch (e) {
- console.error(e);
- return;
- }
- toast.id = options.id;
- toast.tag = 'Toast' + toast.id;
- Notifications.ToastNotificationManager
- .createToastNotifier()
- .addToSchedule(toast);
- },
- /**
- * Schedules a backup local notification 10 years later.
- *
- * @param {Object} notification
- * The local notification
- */
- scheduleBackupNotification: function (notification, options) {
- var properties = Object.create(options);
- properties.id = options.id + '-2';
- properties.at = options.at + 315360000; // 10 years later
- this.scheduleLocalNotification(notification, properties);
- },
- /**
- * Updates the badge number of the active tile.
- *
- * @param {Number} badge
- * The badge number. Zero will clean the badge.
- */
- updateBadge: function (badge) {
- var notifications = Windows.UI.Notifications,
- type = notifications.BadgeTemplateType.badgeNumber,
- xml = notifications.BadgeUpdateManager.getTemplateContent(type),
- attrs = xml.getElementsByTagName('badge'),
- notification = new notifications.BadgeNotification(xml);
- attrs[0].setAttribute('value', badge);
- notifications.BadgeUpdateManager.createBadgeUpdaterForApplication()
- .update(notification);
- },
- /**
- * Updates existing notifications specified by IDs in options.
- *
- * @param {Object[]} notifications
- * Array of local notifications
- */
- update: function (notifications) {
- for (var i = 0; i < notifications.length; i++) {
- var updates = notifications[i],
- options = getAll(updates.id || '0')[0];
- this.updateLocalNotification(options, updates);
- this.fireEvent('update', options);
- }
- },
- /**
- * Updates a single local notification.
- *
- * @param {Object} notification
- * The local notification
- * @param {Object} updates
- * Updated properties
- */
- updateLocalNotification: function (notification, updates) {
- for (var key in updates) {
- notification[key] = updates[key];
- }
- this.cancelLocalNotification(notification.id);
- this.scheduleLocalNotification(notification);
- },
- /**
- * Clears the specified notifications.
- *
- * @param {int[]} ids
- * List of local notification IDs
- */
- clear: function (ids) {
- for (var i = 0; i < ids.length; i++) {
- var id = ids[i],
- notification = this.getAll([id])[0];
- this.clearLocalNotification(id);
- this.fireEvent('clear', notification);
- }
- },
- /**
- * Clears the local notification with the given ID.
- *
- * @param {String} id
- * Local notification ID
- */
- clearLocalNotification: function (id) {
- var notification = this.getAll([id])[0];
- this.getToastHistory().remove('Toast' + id);
- if (this.isRepeating(notification))
- return;
- if (this.isTriggered(id) && !this.isScheduled(id)) {
- this.cancelLocalNotification(id);
- }
- },
- /**
- * Clears all notifications.
- */
- clearAll: function () {
- var ids = this.getTriggeredIds();
- for (var i = 0; i < ids.length; i++) {
- this.clearLocalNotification(ids[i]);
- }
- this.getToastHistory().clear();
- this.fireEvent('clearall');
- },
- /**
- * Cancels all specified notifications.
- *
- * @param {int[]} ids
- * List of local notification IDs
- */
- cancel: function (ids) {
- for (var i = 0; i < ids.length; i++) {
- var id = ids[i],
- notification = this.getAll([id])[0];
- this.cancelLocalNotification(ids[i]);
- this.fireEvent('cancel', notification);
- }
- },
- /**
- * Cancels the local notification with the given ID.
- *
- * @param {String} id
- * Local notification ID
- */
- cancelLocalNotification: function (id) {
- var notifier = this.getToastNotifier(),
- history = this.getToastHistory(),
- toasts = this.getScheduledToasts();
- history.remove('Toast' + id);
- for (var i = 0; i < toasts.length; i++) {
- var toast = toasts[i];
- if (toast.id == id || toast.id == id + '-2') {
- notifier.removeFromSchedule(toast);
- }
- }
- },
- /**
- * Cancels all notifications.
- */
- cancelAll: function () {
- var ids = this.getAllIds();
- for (var i = 0; i < ids.length; i++) {
- this.cancelLocalNotification(ids[i]);
- }
- this.getToastHistory().clear();
- this.fireEvent('cancelall');
- },
- /**
- * Checks if a notification with an ID is present.
- *
- * @param {int} id
- * Local notification ID
- */
- isPresent: function (id) {
- return !!this.findToastById(id);
- },
- /**
- * Checks if a notification with an ID was scheduled.
- *
- * @param {int} id
- * Local notification ID
- */
- isScheduled: function (id) {
- var toast = this.findToastById(id);
- return toast && this.isToastScheduled(toast);
- },
- /**
- * Checks if a notification with an ID was triggered.
- *
- * @param {int} id
- * Local notification ID
- */
- isTriggered: function (id) {
- return this.getTriggeredIds().indexOf(id) > -1;
- },
- /**
- * Lists all local notification IDs.
- */
- getAllIds: function () {
- var toasts = this.getScheduledToasts(),
- ids = [];
- for (var i = 0; i < toasts.length; i++) {
- var toast = toasts[i];
- ids.push(this.getToastId(toast));
- }
- return ids;
- },
- /**
- * Lists all scheduled notification IDs.
- */
- getScheduledIds: function () {
- var toasts = this.getScheduledToasts(),
- ids = [];
- for (var i = 0; i < toasts.length; i++) {
- var toast = toasts[i];
- if (!this.isToastScheduled(toast))
- continue;
- ids.push(this.getToastId(toast));
- }
- return ids;
- },
- /**
- * Lists all scheduled notification IDs.
- */
- getTriggeredIds: function () {
- var toasts = this.getScheduledToasts(),
- ids = [];
- for (var i = 0; i < toasts.length; i++) {
- var toast = toasts[i];
- if (!this.isToastTriggered(toast))
- continue;
- ids.push(this.getToastId(toast));
- }
- return ids;
- },
- /**
- * Property list for given notifications.
- * If called without IDs, all notification will be returned.
- *
- * @param {int[]} ids
- * List of local notification IDs.
- * @param {String?} type
- * Local notification life cycle type
- */
- getAll: function (ids, type) {
- var toasts = this.getScheduledToasts(),
- notifications = [];
- if (!ids || ids.length === 0) {
- ids = this.getAllIds();
- }
- for (var index = 0; index < ids.length; index++) {
- var id = ids[index],
- toast = this.findToastById(id);
- if (!toast || type && this.getToastType(toast) != type)
- continue;
- var json = toast.content.lastChild.lastChild.innerText;
- notifications.push(JSON.parse(json));
- }
- return notifications;
- },
- /**
- * Property list for given notifications.
- * If called without IDs, all notification will be returned.
- *
- * @param {int[]} ids
- * List of local notification IDs
- */
- getScheduled: function (ids) {
- if (!ids || ids.length === 0) {
- ids = this.getAllIds();
- }
- return this.getAll(ids, 'scheduled');
- },
- /**
- * Property list for given notifications.
- * If called without IDs, all notification will be returned.
- *
- * @param {int[]} ids
- * List of local notification IDs
- */
- getTriggered: function (ids) {
- if (!ids || ids.length === 0) {
- ids = this.getAllIds();
- }
- return this.getAll(ids, 'triggered');
- },
- };
|