Explorar o código

Add method to retrieve the application state on each platform

Sebastián Katzer %!s(int64=11) %!d(string=hai) anos
pai
achega
715484fc51
Modificáronse 2 ficheiros con 31 adicións e 7 borrados
  1. 11 1
      src/android/LocalNotification.java
  2. 20 6
      src/ios/APPLocalNotification.m

+ 11 - 1
src/android/LocalNotification.java

@@ -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
src/ios/APPLocalNotification.m

@@ -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)",