Explorar o código

Register trigger handler for each scheduled notification on launch

Sebastián Katzer %!s(int64=10) %!d(string=hai) anos
pai
achega
049680a95d
Modificáronse 2 ficheiros con 44 adicións e 20 borrados
  1. 6 8
      src/windows/LocalNotificationCore.js
  2. 38 12
      src/windows/LocalNotificationUtil.js

+ 6 - 8
src/windows/LocalNotificationCore.js

@@ -32,14 +32,12 @@ exports.core = {
      */
     deviceready: function () {
         var plugin = cordova.plugins.notification.local,
-            args;
+            events = this.eventQueue;
 
         this.isReady = true;
 
-        for (var i = 0; i < this.eventQueue.length; i++) {
-            args = this.eventQueue[i];
-
-            plugin.fireEvent.apply(plugin, args);
+        for (var i = 0; i < events.length; i++) {
+            plugin.fireEvent.apply(plugin, events[i]);
         }
 
         this.eventQueue = [];
@@ -370,7 +368,7 @@ exports.core = {
         var toasts = this.getScheduledToasts(),
             notifications = [];
 
-        if (ids.length === 0) {
+        if (!ids || ids.length === 0) {
             ids = this.getAllIds();
         }
 
@@ -397,7 +395,7 @@ exports.core = {
      *      List of local notification IDs
      */
     getScheduled: function (ids) {
-        if (ids.length === 0) {
+        if (!ids || ids.length === 0) {
             ids = this.getAllIds();
         }
 
@@ -412,7 +410,7 @@ exports.core = {
      *      List of local notification IDs
      */
     getTriggered: function (ids) {
-        if (ids.length === 0) {
+        if (!ids || ids.length === 0) {
             ids = this.getAllIds();
         }
 

+ 38 - 12
src/windows/LocalNotificationUtil.js

@@ -19,6 +19,9 @@
     under the License.
 */
 
+
+var channel = require('cordova/channel');
+
 exports = require('de.appplant.cordova.plugin.local-notification.LocalNotification.Proxy.Core').core;
 
 
@@ -288,6 +291,20 @@ exports.callOnTrigger = function (notification, callback) {
     });
 };
 
+/**
+ * Sets trigger event for all scheduled local notification.
+ *
+ * @param {Function} callback
+ *      Callback function
+ */
+exports.callOnTriggerForScheduled = function (callback) {
+    var notifications = this.getScheduled();
+
+    for (var i = 0; i < notifications.length; i++) {
+        this.callOnTrigger(notifications[i], callback);
+    }
+};
+
 /**
  * The application state - background or foreground.
  *
@@ -328,6 +345,27 @@ exports.fireEvent = function (event, notification) {
  * LIFE CYCLE *
  **************/
 
+// Called before 'deviceready' event
+channel.onCordovaReady.subscribe(function () {
+    // Register trigger handler for each scheduled notification
+    exports.callOnTriggerForScheduled(function (notification) {
+        this.updateBadge(notification.badge);
+        this.fireEvent('trigger', notification);
+    });
+});
+
+// Handle onclick event
+WinJS.Application.addEventListener('activated', function (args) {
+    var id = args.detail.arguments,
+        notification = exports.getAll([id])[0];
+
+    if (!notification)
+        return;
+
+    exports.clearLocalNotification(id);
+    exports.fireEvent('click', notification);
+}, false);
+
 // App is running in background
 document.addEventListener('pause', function () {
     exports.isInBackground = true;
@@ -342,15 +380,3 @@ 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];
-
-    if (!notification)
-        return;
-
-    exports.clearLocalNotification(id);
-    exports.fireEvent('click', notification);
-}, false);