-
Notifications
You must be signed in to change notification settings - Fork 139
/
PushRuntime.m
158 lines (122 loc) · 6.33 KB
/
PushRuntime.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
//
// PushRuntime.m
// Pushwoosh Phonegap SDK
// (c) Pushwoosh 2012
//
#import "PushRuntime.h"
#import "PushNotificationManager.h"
#import <objc/runtime.h>
#import "PushNotification.h"
#import "PW_SBJsonParser.h"
#import "PW_SBJsonWriter.h"
@implementation AppDelegate(Pushwoosh)
- (void)application:(UIApplication *)application internalDidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
PushNotification *pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
[pushHandler.pushManager handlePushRegistration:devToken];
//you might want to send it to your backend if you use remote integration
NSString *token = [pushHandler.pushManager getPushToken];
NSLog(@"Push token: %@", token);
}
- (void)application:(UIApplication *)application newDidRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
[self application:application newDidRegisterForRemoteNotificationsWithDeviceToken:devToken];
[self application:application internalDidRegisterForRemoteNotificationsWithDeviceToken:devToken];
}
- (void)application:(UIApplication *)application internalDidFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
PushNotification* pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
[pushHandler onDidFailToRegisterForRemoteNotificationsWithError:err];
}
- (void)application:(UIApplication *)application newDidFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
[self application:application newDidFailToRegisterForRemoteNotificationsWithError:err];
[self application:application internalDidFailToRegisterForRemoteNotificationsWithError:err];
}
- (void)application:(UIApplication *)application internalDidReceiveRemoteNotification:(NSDictionary *)userInfo {
PushNotification *pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
[pushHandler.pushManager handlePushReceived:userInfo];
}
- (void)application:(UIApplication *)application newDidReceiveRemoteNotification:(NSDictionary *)userInfo {
[self application:application newDidReceiveRemoteNotification:userInfo];
[self application:application internalDidReceiveRemoteNotification:userInfo];
}
- (BOOL)application:(UIApplication *)application newDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
BOOL result = [self application:application newDidFinishLaunchingWithOptions:launchOptions];
PushNotification *pushHandler = [self.viewController getCommandInstance:@"PushNotification"];
if(!pushHandler || !pushHandler.pushManager)
return result;
[pushHandler.pushManager sendAppOpen];
if(result) {
NSDictionary * userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[pushHandler.pushManager handlePushReceived:userInfo];
if(userInfo) {
NSMutableDictionary *pn = [NSMutableDictionary dictionaryWithDictionary:userInfo];
//convert userdata from JSON string to JSON Object
NSString* u = [userInfo objectForKey:@"u"];
if (u) {
PW_SBJsonParser * json = [[PW_SBJsonParser alloc] init];
NSDictionary *dict = [json objectWithString:u];
json = nil;
if (dict) {
[pn setObject:dict forKey:@"u"];
}
}
[pn setValue:[NSNumber numberWithBool:YES] forKey:@"onStart"];
PW_SBJsonWriter * json = [[PW_SBJsonWriter alloc] init];
NSString *jsonString = [json stringWithObject:pn];
json = nil;
//the webview is not loaded yet, keep it for the callback
pushHandler.startPushData = jsonString;
}
}
return result;
}
void dynamicMethodIMP(id self, SEL _cmd, id application, id param) {
if (_cmd == @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)) {
[self application:application internalDidRegisterForRemoteNotificationsWithDeviceToken:param];
return;
}
if (_cmd == @selector(application:didFailToRegisterForRemoteNotificationsWithError:)) {
[self application:application internalDidFailToRegisterForRemoteNotificationsWithError:param];
return;
}
if (_cmd == @selector(application:didReceiveRemoteNotification:)) {
[self application:application internalDidReceiveRemoteNotification:param];
return;
}
}
+ (void)load {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:)), class_getInstanceMethod(self, @selector(application:newDidFinishLaunchingWithOptions:)));
//if methods does not exist - provide default implementation, otherwise swap the implementation
Method method = nil;
method = class_getInstanceMethod(self, @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:));
if(method) {
method_exchangeImplementations(method, class_getInstanceMethod(self, @selector(application:newDidRegisterForRemoteNotificationsWithDeviceToken:)));
}
else {
class_addMethod(self, @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:), (IMP)dynamicMethodIMP, "v@:::");
}
method = class_getInstanceMethod(self, @selector(application:didFailToRegisterForRemoteNotificationsWithError:));
if(method) {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(application:didFailToRegisterForRemoteNotificationsWithError:)), class_getInstanceMethod(self, @selector(application:newDidFailToRegisterForRemoteNotificationsWithError:)));
}
else {
class_addMethod(self, @selector(application:didFailToRegisterForRemoteNotificationsWithError:), (IMP)dynamicMethodIMP, "v@:::");
}
method = class_getInstanceMethod(self, @selector(application:didReceiveRemoteNotification:));
if(method) {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(application:didReceiveRemoteNotification:)), class_getInstanceMethod(self, @selector(application:newDidReceiveRemoteNotification:)));
}
else {
class_addMethod(self, @selector(application:didReceiveRemoteNotification:), (IMP)dynamicMethodIMP, "v@:::");
}
}
@end
@implementation UIApplication(Pushwoosh)
- (void) pw_setApplicationIconBadgeNumber:(NSInteger) badgeNumber {
[self pw_setApplicationIconBadgeNumber:badgeNumber];
[[PushNotificationManager pushManager] sendBadges:badgeNumber];
}
+ (void) load {
method_exchangeImplementations(class_getInstanceMethod(self, @selector(setApplicationIconBadgeNumber:)), class_getInstanceMethod(self, @selector(pw_setApplicationIconBadgeNumber:)));
UIApplication *app = [UIApplication sharedApplication];
NSLog(@"Initializing application: %@, %@", app, app.delegate);
}
@end