forked from geoloqi/MapAttack-iPhone
/
LQPushHandler.m
219 lines (193 loc) · 7.35 KB
/
LQPushHandler.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//
// LQPushHandler.m
// Geoloqi
//
// Created by Aaron Parecki on 12/23/10.
// Copyright 2010 Geoloqi.com. All rights reserved.
//
#import "LQPushHandler.h"
#import "CJSONSerializer.h"
#import "MapAttack.h"
#import "MapAttackAppDelegate.h"
@implementation LQPushHandler
@synthesize lastAlertURL;
- (id)myInit {
self = [super init];
lastAlertURL = [[NSString alloc] init];
NSLog(@"Alloc url: %@", lastAlertURL);
return self;
}
/**
* This is called from application: didReceiveRemoteNotification: which is called regardless of whether
* the app is in the foreground or background, but only if it is currently running.
*/
- (void)handlePush:(UIApplication *)application notification:(NSDictionary *)userInfo {
NSString *title;
// Push was received while the app was in the foreground
if(application.applicationState == UIApplicationStateActive) {
NSDictionary *location = [userInfo valueForKeyPath:@"lqloc"];
if(location) {
NSLog(@"Got some location data! Yeah!!");
NSDictionary *json = [NSDictionary dictionaryWithObject:[[CJSONSerializer serializer] serializeObject:userInfo] forKey:@"json"];
[[NSNotificationCenter defaultCenter] postNotificationName:LQMapAttackDataNotification
object:self
userInfo:json];
return;
}
}
NSString *type = [userInfo valueForKeyPath:@"geoloqi.type"];
if(type && [@"geonote" isEqualToString:type]) {
title = @"MapAttack";
} else if(type && [@"message" isEqualToString:type]) {
title = @"MapAttack";
} else {
title = @"MapAttack";
}
if([type isEqualToString:@"shutdownPrompt"]){
if([application applicationState] == UIApplicationStateActive) {
// Received a push notification asking the user if they want to shut off location updates.
// If updates have already been turned off, don't bother actually prompting this.
if([[lqAppDelegate socketClient] locationUpdateState]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:[userInfo valueForKeyPath:@"aps.alert.body"]
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alert setTag:kLQPushAlertShutdown];
[alert show];
[alert release];
}
} else {
[[lqAppDelegate socketClient] stopMonitoringLocation];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Geoloqi"
message:@"Location tracking is off"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
}
} else if([type isEqualToString:@"startPrompt"]) {
if([application applicationState] == UIApplicationStateActive) {
// Received a push notification asking the user if they want to turn on location updates.
// If updates are already on, don't bother actually prompting this.
if([[lqAppDelegate socketClient] locationUpdateState] == NO){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:[userInfo valueForKeyPath:@"aps.alert.body"]
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[alert setTag:kLQPushAlertStart];
[alert show];
[alert release];
} else {
[[lqAppDelegate socketClient] startMonitoringLocation];
}
}
} else {
if([userInfo valueForKeyPath:@"aps.alert.body"] != nil) {
UIAlertView *alert;
if([userInfo valueForKeyPath:@"geoloqi.link"] != nil) {
alert = [[UIAlertView alloc] initWithTitle:title
message:[userInfo valueForKeyPath:@"aps.alert.body"]
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:@"View", nil];
self.lastAlertURL = [userInfo valueForKeyPath:@"geoloqi.link"];
NSLog(@"Storing value in lastAlertURL: %@", lastAlertURL);
} else {
alert = [[UIAlertView alloc] initWithTitle:title
message:[userInfo valueForKeyPath:@"aps.alert.body"]
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
self.lastAlertURL = nil;
}
if([application applicationState] == UIApplicationStateActive) {
[alert show];
[alert setTag:kLQPushAlertGeonote];
} else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.lastAlertURL]];
}
[alert release];
}
}
}
- (void)handleLocalNotificationFromApp:(UIApplication *)app notif:(UILocalNotification *)notif {
// A local notification came in but the app was in the foreground. This method is called immediately,
// so we need to show them the alert and handle the response appropriately.
if([app applicationState] == UIApplicationStateActive){
// Don't prompt if tracking is already off. Probably won't happen anymore since the timers are cancelled when tracking is turned off.
if([[lqAppDelegate socketClient] locationUpdateState]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[notif.userInfo objectForKey:@"title"]
message:[notif.userInfo objectForKey:@"description"]
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:notif.alertAction, nil];
[alert show];
[alert setTag:kLQPushAlertShutdown];
[alert release];
}
} else {
// The app just launched and it was running in the background. If they hit "Yes" then shut off updates
[[lqAppDelegate socketClient] stopMonitoringLocation];
}
}
/**
* This is called when the app is launched from the button on a push notification
*/
- (void)handleLaunch:(NSDictionary *)launchOptions {
NSLog(@"---- Handling launch from push notification");
NSDictionary *data = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(data == nil)
return;
NSString *type = [data valueForKeyPath:@"geoloqi.type"];
if([type isEqualToString:@"startPrompt"]){
[[lqAppDelegate socketClient] startMonitoringLocation];
lqAppDelegate.tabBarController.selectedIndex = 1;
}else if([type isEqualToString:@"shutdownPrompt"]){
[[lqAppDelegate socketClient] stopMonitoringLocation];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"MapAttack"
message:@"Location tracking is off"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
}else{
if([data valueForKeyPath:@"geoloqi.link"] != nil) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[data valueForKeyPath:@"geoloqi.link"]]];
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"URL: %@", lastAlertURL);
switch([alertView tag]) {
case kLQPushAlertShutdown:
if(buttonIndex == 1){
// Shut off location tracking now
[[lqAppDelegate socketClient] stopMonitoringLocation];
}
break;
case kLQPushAlertStart:
if(buttonIndex == 1){
// Turn on location tracking now
[[lqAppDelegate socketClient] startMonitoringLocation];
}
break;
case kLQPushAlertGeonote:
if(buttonIndex == 1){
// Clicked "view" and the push contained a link, so open a web browser
NSLog(@"User clicked View, reading the value from lastAlertURL");
NSLog(@"Value: %@", self.lastAlertURL);
if(self.lastAlertURL != nil) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.lastAlertURL]];
}
}
}
}
- (void)dealloc {
[lastAlertURL release];
[super dealloc];
}
@end