-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCDVBackgroundNotification.m
119 lines (100 loc) · 3.98 KB
/
CDVBackgroundNotification.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
////
// CDVBackgroundGeoLocation
//
// Created by Chris Scott <[email protected]> on 2013-06-15
// Largely based upon http://www.mindsizzlers.com/2011/07/ios-background-location/
//
#import "CDVBackgroundNotification.h"
#import <Cordova/CDVJSON.h>
#import "AppDelegate.h"
@implementation AppDelegate(AppDelegate)
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void(^)(UIBackgroundFetchResult result))completionHandler
{
void (^safeHandler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result){
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(result);
});
};
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithCapacity:2];
[params setObject:safeHandler forKey:@"handler"];
[params setObject:userInfo forKey:@"userInfo"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"BackgroundNotification" object:params];
}
@end
@implementation CDVBackgroundNotification
{
void (^_completionHandler)(UIBackgroundFetchResult);
NSString *_callbackId;
NSNotification *_notification;
}
- (void)pluginInitialize
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onNotification:)
name:@"BackgroundNotification"
object:nil];
}
- (void) configure:(CDVInvokedUrlCommand*)command
{
NSLog(@"- CDVBackgroundNotification configure");
UIApplication *app = [UIApplication sharedApplication];
UIApplicationState state = [app applicationState];
_callbackId = command.callbackId;
// Handle case where app was launched due to notification event
if (state == UIApplicationStateBackground && _completionHandler && _notification) {
[self onNotification:_notification];
}
}
-(void) onNotification:(NSNotification *) notification
{
UIApplication *app = [UIApplication sharedApplication];
// We only run in the background. Foreground notifications should already be handled.
UIApplicationState state = [app applicationState];
if (state != UIApplicationStateBackground) {
return;
}
NSLog(@"- CDVBackgroundNotification onNotification");
_notification = notification;
_completionHandler = [notification.object[@"handler"] copy];
[self.commandDelegate runInBackground:^{
CDVPluginResult* result = nil;
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary: notification.object[@"userInfo"]];
[result setKeepCallbackAsBool:YES];
// Inform javascript a background-fetch event has occurred.
[self.commandDelegate sendPluginResult:result callbackId:_callbackId];
}];
}
-(void) finish:(CDVInvokedUrlCommand*)command
{
[self doFinish];
}
-(void) doFinish
{
UIApplication *app = [UIApplication sharedApplication];
float finishTimer = (app.backgroundTimeRemaining > 20.0) ? 20.0 : app.backgroundTimeRemaining;
[NSTimer scheduledTimerWithTimeInterval:finishTimer
target:self
selector:@selector(stopBackgroundTask:)
userInfo:nil
repeats:NO];
}
-(void)stopBackgroundTask:(NSTimer*)timer
{
UIApplication *app = [UIApplication sharedApplication];
if (_completionHandler) {
NSLog(@"- CDVBackgroundNotification stopBackgroundTask (remaining t: %f)", app.backgroundTimeRemaining);
_completionHandler(UIBackgroundFetchResultNewData);
_completionHandler = nil;
}
}
// If you don't stopMonitorying when application terminates, the app will be awoken still when a
// new location arrives, essentially monitoring the user's location even when they've killed the app.
// Might be desirable in certain apps.
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end