Ver Fonte

Added the ability to override notifications default properties

Sebastián Katzer há 12 anos atrás
pai
commit
afc7f25099
2 ficheiros alterados com 104 adições e 35 exclusões
  1. 18 0
      README.md
  2. 86 35
      www/local-notification.js

+ 18 - 0
README.md

@@ -49,6 +49,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/356).
 #### Version 0.7.0 (not yet released)
 - **Note:** The new way of callback registration will be not compatible with previous versions! See #62
 - [feature:] Added new callback registration interface and new callback types.
+- [feature:] Added the ability to override notifications default properties.
 
 #### Version 0.7.0beta1 (17.01.2014)
 - [bugfix:] App throws an error on iOS if `message` is null.
@@ -149,6 +150,18 @@ There are 4 different callback types available. For each of them one listener ca
 window.plugin.notification.local.on_callback_ = function (id, state, json) {};
 ```
 
+### getDefaults()
+Gives an overview about all available notification properties for the platform and their default values. The function returns an object.
+```javascript
+window.plugin.notification.local.getDefaults();
+```
+
+### setDefaults ()
+Overrides the default properties. The function takes an object as argument.
+```javascript
+window.plugin.notification.local.setDefaults(Object);
+```
+
 
 ## Examples
 #### Will fire every week on this day, 60 seconds from now
@@ -193,6 +206,11 @@ window.plugin.notification.local.onclick = function (id, state, json) {
 }
 ```
 
+### Change the default value for autoCancel
+```javascript
+window.plugin.notification.local.setDefaults({ autoCancel: true });
+```
+
 
 ## Platform specifics
 ### Small and large icons on Android

+ 86 - 35
www/local-notification.js

@@ -20,59 +20,106 @@
 */
 
 var LocalNotification = function () {
-
+    this._deafults = {
+        message:    '',
+        title:      '',
+        autoCancel: false,
+        ongoing:    false,
+        badge:      0,
+        id:         '0',
+        json:       '',
+        repeat:     ''
+    };
 };
 
 LocalNotification.prototype = {
     /**
-     * Fügt einen neuen Eintrag zur Registry hinzu.
+     * Gibt alle Standardeinstellungen an.
+     *
+     * @return {Object}
+     */
+    getDefaults: function () {
+        return this._deafults;
+    },
+
+    /**
+     * Überschreibt die Standardeinstellungen.
+     *
+     * @param {Object} defaults
+     */
+    setDefaults: function (newDefaults) {
+        var defaults = this.getDefaults();
+
+        for (var key in defaults) {
+            if (newDefaults[key] !== undefined) {
+                defaults[key] = newDefaults[key];
+            }
+        }
+    },
+
+    /**
+     * @private
+     * Merged die Eigenschaften mit den Standardwerten.
      *
      * @param {Object} options
-     * @return {Number} Die ID der Notification
+     * @retrun {Object}
      */
-    add: function (options) {
-        var defaults = {
-            date:       new Date(),
-            message:    '',
-            title:      '',
-            autoCancel: false,
-            ongoing:    false,
-            badge:      0,
-            id:         '0',
-            json:       '',
-            repeat:     ''
-        };
+    mergeWithDefaults: function (options) {
+        var defaults = this.getDefaults();
+
+        for (var key in defaults) {
+            if (options[key] === undefined) {
+                options[key] = defaults[key];
+            }
+        }
+
+        return options;
+    },
+
+    /**
+     * @private
+     */
+    applyPlatformSpecificOptions: function () {
+        var defaults = this._deafults;
 
         switch (device.platform) {
-            case 'Android':
-                defaults.icon = 'icon';
-                defaults.smallIcon = null;
-                defaults.sound = 'TYPE_NOTIFICATION'; break;
-            case 'iOS':
-                defaults.sound = ''; break;
-            case 'WinCE': case 'Win32NT':
-                defaults.smallImage = null;
-                defaults.image = null;
-                defaults.wideImage = null;
+        case 'Android':
+            defaults.icon      = 'icon';
+            defaults.smallIcon = null;
+            defaults.sound     = 'TYPE_NOTIFICATION'; break;
+        case 'iOS':
+            defaults.sound = ''; break;
+        case 'WinCE': case 'Win32NT':
+            defaults.smallImage = null;
+            defaults.image      = null;
+            defaults.wideImage  = null;
         };
+    },
 
-        for (var key in defaults) {
-            if (options[key] !== undefined) {
-                defaults[key] = options[key];
-            }
+    /**
+     * Fügt einen neuen Eintrag zur Registry hinzu.
+     *
+     * @param {Object} options
+     * @return {Number} Die ID der Notification
+     */
+    add: function (options) {
+        var options = this.mergeWithDefaults(options);
+
+        if (options.id) {
+            options.id = options.id.toString();
         }
 
-        if (defaults.id) {
-            defaults.id = defaults.id.toString();
+        if (options.date === undefined) {
+            options.date = new Date();
         }
 
-        if (typeof defaults.date == 'object') {
-            defaults.date = Math.round(defaults.date.getTime()/1000);
+        if (typeof options.date == 'object') {
+            options.date = Math.round(options.date.getTime()/1000);
         }
 
-        cordova.exec(null, null, 'LocalNotification', 'add', [defaults]);
+        cordova.exec(null, null, 'LocalNotification', 'add', [options]);
 
-        return defaults.id;
+        return options.id;
     },
 
     /**
@@ -130,4 +177,8 @@ LocalNotification.prototype = {
 
 var plugin = new LocalNotification();
 
+document.addEventListener('deviceready', function () {
+    plugin.applyPlatformSpecificOptions();
+}, false);
+
 module.exports = plugin;