-
Notifications
You must be signed in to change notification settings - Fork 29
/
RemotePushDelegate.m
86 lines (67 loc) · 2.94 KB
/
RemotePushDelegate.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
#import "RemotePushDelegate.h"
#import <objc/runtime.h>
@implementation AppDelegate(RemotePushDelegate)
NSDictionary *launchNotification;
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:deviceToken forKey:@"deviceToken"];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"registeredForRemoteNotifications"
object:self userInfo:userInfo];
}
+ (void)load
{
Method original, swizzled;
original = class_getInstanceMethod(self, @selector(init));
swizzled = class_getInstanceMethod(self, @selector(swizzled_init));
method_exchangeImplementations(original, swizzled);
}
- (AppDelegate *)swizzled_init
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createNotificationChecker:) name:@"UIApplicationDidFinishLaunchingNotification" object:nil];
// This actually calls the original init method over in AppDelegate. Equivilent to calling super
// on an overrided method, this is not recursive, although it appears that way. neat huh?
return [self swizzled_init];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
// TODO Handle this on Manager side
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:error forKey:@"deviceToken"];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"registeredForRemoteNotificationsError"
object:self userInfo:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
UIApplicationState appState = UIApplicationStateActive;
if ([application respondsToSelector:@selector(applicationState)]) {
appState = application.applicationState;
}
if (appState == UIApplicationStateActive) {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"receivedRemoteNotification"
object:self userInfo:userInfo];
} else {
//save it for later
launchNotification = userInfo;
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (launchNotification) {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"receivedRemoteNotification"
object:self userInfo:launchNotification];
launchNotification = nil;
}
}
- (void)createNotificationChecker:(NSNotification *)notification
{
if (notification)
{
NSDictionary *launchOptions = [notification userInfo];
if (launchOptions) {
launchNotification = [launchOptions objectForKey: @"UIApplicationLaunchOptionsRemoteNotificationKey"];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"receivedRemoteNotificationOnStart"
object:self userInfo:launchNotification];
}
}
}
@end