Эх сурвалжийг харах

Added `json` property to pass custom data through the notification

Sebastián Katzer 12 жил өмнө
parent
commit
c05c4e1484

+ 16 - 1
README.md

@@ -57,6 +57,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/331).
 - **[bugfix:]** Callbacks are called with the ID as a number and not as a string.
 - [enhancement:] The background callback on Android is called, even the app is not running when the notification is tapped.
 - [enhancement:] Notifications are repeated more precisely.
+- [feature:] Added `json` property to pass custom data through the notification.
 
 #### Version 0.6.3 (12.12.2013)
 - [bugfix:] Black screen on Android.
@@ -112,7 +113,8 @@ window.plugin.notification.local.add({
     title:      String,  // The title of the message
     repeat:     String,  // Has the options of 'hourly', 'daily', 'weekly', 'monthly', 'yearly'
     badge:      Number,  // Displays number badge to notification
-    sound:      String,  // A sound to be played (iOS & Android)
+    sound:      String,  // A sound to be played
+    json:       String,  // Data to be passed through the notification
     autoCancel: Boolean, // Setting this flag and the notification is automatically canceled when the user clicks it
     foreground: String,  // A javascript function to be called if the app is running
     background: String,  // A javascript function to be called if the app is in the background
@@ -165,6 +167,19 @@ window.plugin.notification.local.add({ message: 'Great app!' });
 ```javascript
 window.plugin.notification.local.add({ sound: null });
 ```
+#### Pass data through the notification
+```javascript
+window.plugin.notification.local.add({
+    id:         1,
+    message:    'I love BlackBerry!',
+    json:       { test: 123 },
+    foreground: 'forground'
+});
+
+function foreground (id, json) {
+    console.log(id, JSON.parse(json).test);
+}
+```
 
 
 ## Platform specifics

+ 7 - 0
src/android/Options.java

@@ -208,6 +208,13 @@ public class Options {
         return options.optBoolean("autoCancel", false);
     }
 
+    /**
+     * Gibt die zusätzlichen Daten als String an.
+     */
+    public String getJSON () {
+        return options.optString("json", "");
+    }
+
     /**
      * Gibt den Zahlwert des Icons an.
      *

+ 4 - 1
src/android/Receiver.java

@@ -185,7 +185,10 @@ public class Receiver extends BroadcastReceiver {
         // after reboot, LocalNotification.webView is always null
         // may be call foreground callback later
         if (!TextUtils.isEmpty(function) && LocalNotification.webView != null) {
-            LocalNotification.webView.sendJavascript("setTimeout('" + function + "(\"" + options.getId() + "\")',0)");
+            String params = "\"" + options.getId() + "\",\\'" + JSONObject.quote(options.getJSON()) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
+            String js     = "setTimeout('" + function + "(" + params + ")',0)";
+
+            LocalNotification.webView.sendJavascript(js);
         }
     }
 }

+ 2 - 1
src/android/ReceiverActivity.java

@@ -79,7 +79,8 @@ public class ReceiverActivity extends Activity {
         if (TextUtils.isEmpty(function))
             return;
 
-        final String js = "setTimeout('" + function + "(\"" + options.getId() + "\")',0)";
+        String params   = "\"" + options.getId() + "\",\\'" + JSONObject.quote(options.getJSON()) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
+        final String js = "setTimeout('" + function + "(" + params + ")',0)";
 
         // after reboot, LocalNotification.webView is always null
         // call background callback later

+ 11 - 4
src/ios/APPLocalNotification.m

@@ -41,7 +41,6 @@
 // Schlüssel-Präfix für alle archivierten Meldungen
 NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
 
-
 @implementation APPLocalNotification
 
 /**
@@ -167,8 +166,14 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
     NSString* bg = [options objectForKey:@"background"];
     NSString* fg = [options objectForKey:@"foreground"];
     NSString* ac = [options objectForKey:@"autoCancel"];
-
-    return [NSDictionary dictionaryWithObjectsAndKeys:id, @"id", bg, @"background", fg, @"foreground", ac, @"autoCancel", nil];
+    NSString* js = [options objectForKey:@"json"];
+
+    return [NSDictionary dictionaryWithObjectsAndKeys:
+            id, @"id",
+            bg, @"background",
+            fg, @"foreground",
+            ac, @"autoCancel",
+            js, @"json", nil];
 }
 
 /**
@@ -229,6 +234,7 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
 
     UILocalNotification* notification = [localNotification object];
     NSString* id                      = [notification.userInfo objectForKey:@"id"];
+    NSString* json                    = [notification.userInfo objectForKey:@"json"];
     NSString* callbackType            = isActive ? @"foreground" : @"background";
     NSString* callbackFn              = [notification.userInfo objectForKey:callbackType];
     BOOL autoCancel                   = [[notification.userInfo objectForKey:@"autoCancel"] boolValue];
@@ -240,7 +246,8 @@ NSString *const kAPP_LOCALNOTIFICATION = @"APP_LOCALNOTIFICATION";
 
     if (![self strIsNullOrEmpty:callbackFn])
     {
-        NSString* js = [NSString stringWithFormat:@"setTimeout('%@(\"%@\")',0)", callbackFn, id];
+        NSString* params = [NSString stringWithFormat:@"\"%@\",\\'%@\\'", id, json];
+        NSString* js     = [NSString stringWithFormat:@"setTimeout('%@(%@)',0)", callbackFn, params];
 
         [self.commandDelegate evalJs:js];
     }

+ 6 - 0
src/wp8/Options.cs

@@ -74,6 +74,12 @@ namespace De.APPPlant.Cordova.Plugin.LocalNotification
         [DataMember(IsRequired = false, Name = "repeat")]
         public string Repeat { get; set; }
 
+        /// <summary>
+        /// Notification specific data
+        /// </summary>
+        [DataMember(IsRequired = false, Name = "json")]
+        public string JSON { get; set; }
+
         /// <summary>
         /// Message-ID
         /// </summary>

+ 1 - 0
www/local-notification.js

@@ -38,6 +38,7 @@ LocalNotification.prototype = {
             autoCancel: false,
             badge:      0,
             id:         '0',
+            json:       '',
             repeat:     '',
             background: '',
             foreground: ''