Преглед изворни кода

Handle click event on windows platform

Sebastián Katzer пре 10 година
родитељ
комит
89be6b9d8c

+ 57 - 42
src/windows/LocalNotificationCore.js

@@ -27,8 +27,23 @@ var Notifications = Windows.UI.Notifications,
 
 exports.core = {
 
-    // Array of unactivated notifications
-    unactivatedIds: [],
+    /**
+     * Executes all queued events.
+     */
+    deviceready: function () {
+        var plugin = cordova.plugins.notification.local,
+            args;
+
+        this.isReady = true;
+
+        for (var i = 0; i < this.eventQueue.length; i++) {
+            args = this.eventQueue[i];
+
+            plugin.fireEvent.apply(plugin, args);
+        }
+
+        this.eventQueue = [];
+    },
 
     /**
      * Schedules new local notifications.
@@ -124,42 +139,6 @@ exports.core = {
 
     },
 
-    /**
-     * 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.getScheduledToast();
-
-        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);
-            }
-        }
-    },
-
-    /**
-     * Clears the local notification with the given ID.
-     *
-     * @param {String} id
-     *      Local notification ID
-     */
-    clearLocalNotification: function (id) {
-        this.getToastHistory().remove('Toast' + id);
-
-        if (this.isTriggered(id) && !this.isScheduled(id)) {
-            this.cancelLocalNotification(id);
-        }
-    },
-
     /**
      * Updates existing notifications specified by IDs in options.
      *
@@ -191,6 +170,20 @@ exports.core = {
         }
     },
 
+    /**
+     * Clears the local notification with the given ID.
+     *
+     * @param {String} id
+     *      Local notification ID
+     */
+    clearLocalNotification: function (id) {
+        this.getToastHistory().remove('Toast' + id);
+
+        if (this.isTriggered(id) && !this.isScheduled(id)) {
+            this.cancelLocalNotification(id);
+        }
+    },
+
     /**
      * Clears all notifications.
      */
@@ -220,6 +213,28 @@ exports.core = {
         }
     },
 
+    /**
+     * 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.
      */
@@ -268,7 +283,7 @@ exports.core = {
      * Lists all local notification IDs.
      */
     getAllIds: function () {
-        var toasts = this.getScheduledToast(),
+        var toasts = this.getScheduledToasts(),
             ids = [];
 
         for (var i = 0; i < toasts.length; i++) {
@@ -284,7 +299,7 @@ exports.core = {
      * Lists all scheduled notification IDs.
      */
     getScheduledIds: function () {
-        var toasts = this.getScheduledToast(),
+        var toasts = this.getScheduledToasts(),
             ids = [];
 
         for (var i = 0; i < toasts.length; i++) {
@@ -303,7 +318,7 @@ exports.core = {
      * Lists all scheduled notification IDs.
      */
     getTriggeredIds: function () {
-        var toasts = this.getScheduledToast(),
+        var toasts = this.getScheduledToasts(),
             ids = [];
 
         for (var i = 0; i < toasts.length; i++) {
@@ -328,7 +343,7 @@ exports.core = {
      *      Local notification life cycle type
      */
     getAll: function (ids, type) {
-        var toasts = this.getScheduledToast(),
+        var toasts = this.getScheduledToasts(),
             notifications = [];
 
         if (ids.length === 0) {

+ 1 - 1
src/windows/LocalNotificationProxy.js

@@ -23,7 +23,7 @@
  * Executes all queued events.
  */
 exports.deviceready  = function () {
-
+    exports.core.deviceready();
 };
 
 /**

+ 26 - 5
src/windows/LocalNotificationUtil.js

@@ -29,6 +29,11 @@ exports = require('de.appplant.cordova.plugin.local-notification.LocalNotificati
 // True if App is running, false if suspended
 exports.isInBackground = true;
 
+// Indicates if the device is ready (to receive events)
+exports.isReady = false;
+
+// Queues all events before deviceready
+exports.eventQueue = [];
 
 /********
  * UTIL *
@@ -158,7 +163,7 @@ exports.getToastNotifier = function () {
  *
  * @return Array
  */
-exports.getScheduledToast = function () {
+exports.getScheduledToasts = function () {
     return this.getToastNotifier().getScheduledToastNotifications();
 };
 
@@ -229,7 +234,7 @@ exports.isToastTriggered = function (toast) {
  * @param Object
  */
 exports.findToastById = function (id) {
-    var toasts = this.getScheduledToastNotifications();
+    var toasts = this.getScheduledToasts();
 
     for (var i = 0; i < toasts.length; i++) {
         var toast = toasts[i];
@@ -284,12 +289,19 @@ exports.getApplicationState = function () {
  */
 exports.fireEvent = function (event, notification) {
     var plugin = cordova.plugins.notification.local.core,
-        state = this.getApplicationState();
+        state = this.getApplicationState(),
+        args;
 
     if (notification) {
-        plugin.fireEvent(event, notification, state);
+        args = [event, notification, state];
+    } else {
+        args = [event, state];
+    }
+
+    if (this.isReady) {
+        plugin.fireEvent.apply(plugin, args);
     } else {
-        plugin.fireEvent(event, state);
+        this.eventQueue.push(args);
     }
 };
 
@@ -312,3 +324,12 @@ document.addEventListener('resume', function () {
 document.addEventListener('deviceready', function () {
     exports.isInBackground = false;
 }, false);
+
+// Handle onclick event
+WinJS.Application.addEventListener('activated', function (args) {
+    var id = args.detail.arguments,
+        notification = exports.getAll([id])[0];
+
+    exports.clearLocalNotification(id);
+    exports.fireEvent('click', notification);
+}, false);