Commit 715484fc by Sebastián Katzer

Add method to retrieve the application state on each platform

parent 2b8b21a7
...@@ -317,7 +317,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -317,7 +317,7 @@ public class LocalNotification extends CordovaPlugin {
* @param {String} json A custom (JSON) string * @param {String} json A custom (JSON) string
*/ */
public static void fireEvent (String event, String id, String json) { 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 params = "\"" + id + "\",\"" + state + "\",\\'" + JSONObject.quote(json) + "\\'.replace(/(^\"|\"$)/g, \\'\\')";
String js = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)"; String js = "setTimeout('plugin.notification.local.on" + event + "(" + params + ")',0)";
...@@ -331,6 +331,16 @@ public class LocalNotification extends CordovaPlugin { ...@@ -331,6 +331,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. * Set the application context if not already set.
*/ */
protected static void setContext (Context context) { protected static void setContext (Context context) {
......
...@@ -49,6 +49,8 @@ ...@@ -49,6 +49,8 @@
- (BOOL) isNotificationScheduledWithId:(NSString*)id; - (BOOL) isNotificationScheduledWithId:(NSString*)id;
// Retrieves the local notification by its ID // Retrieves the local notification by its ID
- (UILocalNotification*) notificationWithId:(NSString*)id; - (UILocalNotification*) notificationWithId:(NSString*)id;
// Retrieves the application state
- (NSString*) applicationState;
// Fires the given event // Fires the given event
- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json; - (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json;
...@@ -514,6 +516,22 @@ ...@@ -514,6 +516,22 @@
} }
/** /**
* 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. * Fires the given event.
* *
* @param {NSString} event * @param {NSString} event
...@@ -525,15 +543,11 @@ ...@@ -525,15 +543,11 @@
*/ */
- (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json - (void) fireEvent:(NSString*)event id:(NSString*)id json:(NSString*)json
{ {
UIApplicationState state = [[UIApplication sharedApplication] NSString* appState = [self applicationState];
applicationState];
bool isActive = state == UIApplicationStateActive;
NSString* stateName = isActive ? @"foreground" : @"background";
NSString* params = [NSString stringWithFormat: NSString* params = [NSString stringWithFormat:
@"\"%@\",\"%@\",\\'%@\\'", @"\"%@\",\"%@\",\\'%@\\'",
id, stateName, json]; id, appState, json];
NSString* js = [NSString stringWithFormat: NSString* js = [NSString stringWithFormat:
@"setTimeout('plugin.notification.local.on%@(%@)',0)", @"setTimeout('plugin.notification.local.on%@(%@)',0)",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment