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

Add `isTriggered` & `getTriggeredIds` methods

parent ceaa0d5d
Loading
Loading
Loading
Loading
+1 −0
Changes for CHANGELOG.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
- [enhancement:] Callbacks for `add`, `cancel` & `cancelAll`
- [enhancement:] `image:` accepts remote URLs and local URIs (Android)
- [feature:] New Android specific `led:` flag.
- [feature:] Add `isTriggered` & `getTriggeredIds` methods.

#### Version 0.7.4 (22.03.2014)
- [bugfix:] Platform specific properties were ignored.
+26 −1
Changes for README.md: 26 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ More informations can be found [here][PGB_plugin].
- [enhancement:] Callbacks for `add`, `cancel` & `cancelAll`
- [enhancement:] `image:` accepts remote URLs and local URIs (Android)
- [feature:] New Android specific `led:` flag
- [feature:] Add `isTriggered` & `getTriggeredIds` methods.

#### Further informations
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
@@ -166,7 +167,7 @@ window.plugin.notification.local.isScheduled(id, function (isScheduled) {
```

### Retrieve the IDs from all currently scheduled local notifications
To retrieve the IDs from all currently scheduled local notifications, the `notification.local.isScheduled` interface can be used.<br>
To retrieve the IDs from all currently scheduled local notifications, the `notification.local.getScheduledIds` interface can be used.<br>
The method takes a callback function to be called with the result as an array of IDs. Optional the scope of the callback can be assigned too.

```javascript
@@ -175,6 +176,29 @@ window.plugin.notification.local.getScheduledIds(function (scheduledIds) {
}, scope);
```

### Check wether a notification with an ID was triggered
To check if a notification with an ID was triggered, the `notification.local.isTriggered` interface can be used.<br>
The method takes the ID of the local notification as an argument and a callback function to be called with the result. Optional the scope of the callback can be assigned too.

#### Further informations
- See [getTriggeredIds][gettriggeredIds] of how to retrieve a list of IDs of all scheduled local notifications.

```javascript
window.plugin.notification.local.isTriggered(id, function (isTriggered) {
    // console.log('Notification with ID ' + id + ' is triggered: ' + isTriggered);
}, scope);
```

### Retrieve the IDs from all currently triggered local notifications
To retrieve the IDs from all currently triggered local notifications, the `notification.local.getTriggeredIds` interface can be used.<br>
The method takes a callback function to be called with the result as an array of IDs. Optional the scope of the callback can be assigned too.

```javascript
window.plugin.notification.local.getTriggeredIds(function (triggeredIds) {
    // alert('Triggered IDs: ' + triggeredIds.join(' ,'));
}, scope);
```

### Get the default values of the local notification properties
The default values of the local notification properties can be retrieved through the `notification.local.getDefaults` interface.<br>
The method returns an object of values for all available local notification properties on the platform.
@@ -500,6 +524,7 @@ This software is released under the [Apache 2.0 License][apache2_license].
[getdefaults]: #get-the-default-values-of-the-local-notification-properties
[setdefaults]: #set-the-default-values-of-the-local-notification-properties
[getscheduledids]: #retrieve-the-ids-from-all-currently-scheduled-local-notifications
[gettriggeredids]: #retrieve-the-ids-from-all-currently-triggered-local-notifications
[isscheduled]: #check-wether-a-notification-with-an-id-is-scheduled
[examples]: #examples
[setdefaults-example]: #change-the-default-value-of-local-notification-properties
+67 −4
Changes for src/android/LocalNotification.java: 67 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@
package de.appplant.cordova.plugin.localnotification;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.Set;

@@ -113,6 +114,16 @@ public class LocalNotification extends CordovaPlugin {
            getScheduledIds(command);
        }

        if (action.equalsIgnoreCase("isTriggered")) {
            String id = args.optString(0);

            isTriggered(id, command);
        }

        if (action.equalsIgnoreCase("getTriggeredIds")) {
            getTriggeredIds(command);
        }

        if (action.equalsIgnoreCase("deviceready")) {
            cordova.getThreadPool().execute( new Runnable() {
                public void run() {
@@ -187,7 +198,7 @@ public class LocalNotification extends CordovaPlugin {
        Intent intent = new Intent(context, Receiver.class)
            .setAction("" + notificationId);

        PendingIntent pi       = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        PendingIntent pi       = PendingIntent.getBroadcast(context, 0, intent, 0);
        AlarmManager am        = getAlarmManager();
        NotificationManager nc = getNotificationManager();

@@ -222,7 +233,7 @@ public class LocalNotification extends CordovaPlugin {
    }

    /**
     * Checks wether a notification with an ID is scheduled.
     * Checks if a notification with an ID is scheduled.
     *
     * @param id
     *          The notification ID to be check.
@@ -246,9 +257,61 @@ public class LocalNotification extends CordovaPlugin {
        SharedPreferences settings = getSharedPreferences();
        Map<String, ?> alarms      = settings.getAll();
        Set<String> alarmIds       = alarms.keySet();
        JSONArray pendingIds       = new JSONArray(alarmIds);
        JSONArray scheduledIds     = new JSONArray(alarmIds);

        command.success(scheduledIds);
    }

    /**
     * Checks if a notification with an ID was triggered.
     *
     * @param id
     *          The notification ID to be check.
     * @param callbackContext
     */
    public static void isTriggered (String id, CallbackContext command) {
        SharedPreferences settings = getSharedPreferences();
        Map<String, ?> alarms      = settings.getAll();
        boolean isScheduled        = alarms.containsKey(id);
        boolean isTriggered        = isScheduled;

        if (isScheduled) {
            JSONObject arguments = (JSONObject) alarms.get(id);
            Options options      = new Options(context).parse(arguments);
            Date fireDate        = new Date(options.getDate());

            isTriggered = new Date().after(fireDate);
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, isTriggered);

        command.sendPluginResult(result);
    }

    /**
     * Retrieves a list with all currently triggered notifications.
     *
     * @param callbackContext
     */
    public static void getTriggeredIds (CallbackContext command) {
        SharedPreferences settings = getSharedPreferences();
        Map<String, ?> alarms      = settings.getAll();
        Set<String> alarmIds       = alarms.keySet();
        JSONArray scheduledIds     = new JSONArray();
        Date now                   = new Date();

        for (String id : alarmIds) {
            JSONObject arguments = (JSONObject) alarms.get(id);
            Options options      = new Options(context).parse(arguments);
            Date fireDate        = new Date(options.getDate());
            boolean isTriggered  = now.after(fireDate);

            if (isTriggered == true) {
                scheduledIds.put(id);
            }
        }

        command.success(pendingIds);
        command.success(scheduledIds);
    }

    /**
+92 −39
Changes for src/ios/APPLocalNotification.m: 92 added lines, 39 removed lines.
Original line number Diff line number Diff line
@@ -21,45 +21,6 @@

#import "APPLocalNotification.h"

@interface APPLocalNotification (Private)

// Schedules a new local notification and fies the coresponding event
- (void) scheduleNotificationWithProperties:(NSMutableDictionary*)properties;
// Cancels the given local notification and fires the cancel event
- (void) cancelNotification:(UILocalNotification*)notification fireEvent:(BOOL)fireEvent;
// Cancels all local notification with are older then
- (void) cancelAllNotificationsWhichAreOlderThen:(float)seconds;
// Retrurns a key-value dictionary for repeat intervals
- (NSMutableDictionary*) repeatDict;
// Returns the userDict for a local notification
- (NSDictionary*) userDict:(NSMutableDictionary*)options;
// Creates an notification object based on the given properties
- (UILocalNotification*) notificationWithProperties:(NSMutableDictionary*)options;
// Calls the cancel or trigger event after a local notification was received
- (void) didReceiveLocalNotification:(NSNotification*)localNotification;
// Calls the cancel or trigger event after a local notification was received
- (void) didFinishLaunchingWithOptions:(NSNotification*)notification;
// Registers obervers for the following events after plugin was initialized.
- (void) pluginInitialize;
// Clears all single repeating notifications which are older then 5 days
- (void) onAppTerminate;
// Checks weather the given string is empty or not
- (BOOL) stringIsNullOrEmpty:(NSString*)str;
// Checks wether a notification with an ID is scheduled or not
- (BOOL) isNotificationScheduledWithId:(NSString*)id;
// Retrieves the local notification by its ID
- (UILocalNotification*) notificationWithId:(NSString*)id;
// Retrieves the application state
- (NSString*) applicationState;
// Retrieves all scheduled notifications
- (NSArray*) scheduledNotifications;
// Simply invokes the callback without any parameter
- (void) execCallback:(CDVInvokedUrlCommand*)command;
// Fires the given event
- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json;

@end

@interface APPLocalNotification ()

// Retrieves all scheduled notifications
@@ -219,6 +180,63 @@
    }];
}

/**
 * Checks wether a notification with an ID was triggered.
 *
 * @param {NSString} id
 *      The ID of the notification
 * @param callback
 *      The callback function to be called with the result
 */
- (void) isTriggered:(CDVInvokedUrlCommand*)command
{
    [self.commandDelegate runInBackground:^{
        NSArray* arguments = [command arguments];
        NSString* id       = [arguments objectAtIndex:0];
        bool isTriggered   = [self isNotificationTriggeredWithId:id];
        CDVPluginResult* result;

        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                     messageAsBool:isTriggered];

        [self.commandDelegate sendPluginResult:result
                                    callbackId:command.callbackId];
    }];
}

/**
 * Retrieves a list of ids from all currently triggered notifications.
 *
 * @param callback
 *      The callback function to be called with the result
 */
- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
{
    [self.commandDelegate runInBackground:^{
        NSArray* notifications = self.scheduledNotifications;

        NSMutableArray* scheduledIds = [[NSMutableArray alloc] init];
        CDVPluginResult* result;

        for (UILocalNotification* notification in notifications)
        {
            if (![self isNotificationTriggered:notification]) {
                continue;
            }

            NSString* id = [notification.userInfo objectForKey:@"id"];

            [scheduledIds addObject:id];
        }

        result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
                                    messageAsArray:scheduledIds];

        [self.commandDelegate sendPluginResult:result
                                    callbackId:command.callbackId];
    }];
}

#pragma mark -
#pragma mark Plugin core methods

@@ -502,6 +520,41 @@
    return notification != NULL;
}

/**
 * Checks wether a notification with an ID was triggered or not.
 *
 * @param id
 *      The ID of the notification
 * @return BOOL
 */
- (BOOL) isNotificationTriggeredWithId:(NSString*)id
{
    UILocalNotification* notification = [self notificationWithId:id];

    if (notification == NULL) {
        return NO;
    }

    return [self isNotificationTriggered:notification];
}

/**
 * Checks wether a notification was triggered or not.
 *
 * @param notification
 *      The notification
 * @return BOOL
 */
- (BOOL) isNotificationTriggered:(UILocalNotification*)notification
{
    NSDate* now      = [NSDate date];
    NSDate* fireDate = notification.fireDate;

    bool isLaterThanOrEqualTo = !([now compare:fireDate] == NSOrderedAscending);

    return isLaterThanOrEqualTo;
}

/**
 * Retrieves the local notification by its ID.
 *
+16 −0
Changes for src/wp8/LocalNotification.cs: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -133,6 +133,22 @@ namespace Cordova.Extension.Commands
            DispatchCommandResult();
        }

        /// <summary>
        /// Checks wether a notification with an ID was triggered
        /// </summary>
        public void isTriggered (string jsonArgs)
        {
            DispatchCommandResult();
        }

        /// <summary>
        /// Retrieves a list with all currently triggered notifications
        /// </summary>
        public void getTriggeredIds (string jsonArgs)
        {
            DispatchCommandResult();
        }

        /// <summary>
        /// Informs that the device is ready and the deviceready event has been fired
        /// </summary>
Loading