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

Add method to retrieve the application state on each platform

parent 2b8b21a7
Loading
Loading
Loading
Loading
+11 −1
Changes for src/android/LocalNotification.java: 11 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -317,7 +317,7 @@ public class LocalNotification extends CordovaPlugin {
     * @param {String} json  A custom (JSON) string
     */
    public static void fireEvent (String event, String id, String json) {
        String state  = isInBackground ? "background" : "foreground";
        String state  = getApplicationState();
        String params = "\"" + id + "\",\"" + state + "\",\\'" + JSONObject.quote(json) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
        String js     = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)";

@@ -330,6 +330,16 @@ public class LocalNotification extends CordovaPlugin {
        }
    }

    /**
     * Retrieves the application state
     *
     * @return {String}
     *      Either "background" or "foreground"
     */
    protected static String getApplicationState () {
        return isInBackground ? "background" : "foreground";
    }

    /**
     * Set the application context if not already set.
     */
+20 −6
Changes for src/ios/APPLocalNotification.m: 20 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@
- (BOOL) isNotificationScheduledWithId:(NSString*)id;
// Retrieves the local notification by its ID
- (UILocalNotification*) notificationWithId:(NSString*)id;
// Retrieves the application state
- (NSString*) applicationState;
// Fires the given event
- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json;

@@ -513,6 +515,22 @@
    return NULL;
}

/**
 * Retrieves the application state
 *
 * @return {NSString}
 *      Either "background" or "foreground"
 */
- (NSString*) applicationState
{
    UIApplicationState state = [[UIApplication sharedApplication]
                                applicationState];

    bool isActive = state == UIApplicationStateActive;

    return isActive ? @"foreground" : @"background";
}

/**
 * Fires the given event.
 *
@@ -525,15 +543,11 @@
 */
- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json
{
    UIApplicationState state = [[UIApplication sharedApplication]
                                applicationState];

    bool isActive = state == UIApplicationStateActive;
    NSString* stateName = isActive ? @"foreground" : @"background";
    NSString* appState = [self applicationState];

    NSString* params = [NSString stringWithFormat:
                        @"\"%@\",\"%@\",\\'%@\\'",
                        id, stateName, json];
                        id, appState, json];

    NSString* js = [NSString stringWithFormat:
                    @"setTimeout('plugin.notification.local.on%@(%@)',0)",