Commit c05c4e14 authored by Sebastián Katzer's avatar Sebastián Katzer
Browse files

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

parent 97b5c60b
Loading
Loading
Loading
Loading
+16 −1
Changes for README.md: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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
Changes for src/android/Options.java: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
Changes for src/android/Receiver.java: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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);
        }
    }
}
 No newline at end of file
+2 −1
Changes for src/android/ReceiverActivity.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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
Changes for src/ios/APPLocalNotification.m: 11 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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];
    }
Loading