-
Notifications
You must be signed in to change notification settings - Fork 993
/
Copy pathAppDelegate+FCMPlugin.m
171 lines (144 loc) · 6.25 KB
/
AppDelegate+FCMPlugin.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
//
// AppDelegate+FCMPlugin.m
// TestApp
//
// Created by felipe on 12/06/16.
//
//
#import "AppDelegate+FCMPlugin.h"
#import "FCMPlugin.h"
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
#import "Firebase.h"
@implementation AppDelegate (MCPlugin)
static NSData *lastPush;
//Method swizzling
+ (void)load
{
Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
Method custom = class_getInstanceMethod(self, @selector(application:customDidFinishLaunchingWithOptions:));
method_exchangeImplementations(original, custom);
}
- (BOOL)application:(UIApplication *)application customDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self application:application customDidFinishLaunchingWithOptions:launchOptions];
NSLog(@"DidFinishLaunchingWithOptions");
// Register for remote notifications
// iOS 7.1 or earlier
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
UIRemoteNotificationType allNotificationTypes = (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
[application registerForRemoteNotificationTypes:allNotificationTypes];
} else {
// iOS 8 or later
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
// [START configure_firebase]
[FIRApp configure];
// [END configure_firebase]
// Add observer for InstanceID token refresh callback.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
return YES;
}
// [START receive_message in background]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
NSError *error;
NSDictionary *userInfoMutable = [userInfo mutableCopy];
if (application.applicationState != UIApplicationStateActive) {
NSLog(@"New method with push callback: %@", userInfo);
[userInfoMutable setValue:@(YES) forKey:@"wasTapped"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfoMutable
options:0
error:&error];
NSLog(@"APP WAS CLOSED DURING PUSH RECEPTION Saved data: %@", jsonData);
lastPush = jsonData;
}
}
// [END receive_message in background]
// [START receive_message]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Pring full message.
NSLog(@"%@", userInfo);
NSError *error;
NSDictionary *userInfoMutable = [userInfo mutableCopy];
//USER NOT TAPPED NOTIFICATION
if (application.applicationState == UIApplicationStateActive) {
[userInfoMutable setValue:@(NO) forKey:@"wasTapped"];
NSLog(@"app active");
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfoMutable
options:0
error:&error];
[FCMPlugin.fcmPlugin notifyOfMessage:jsonData];
// app is in background or in stand by (NOTIFICATION WILL BE TAPPED)
}else{
[userInfoMutable setValue:@(YES) forKey:@"wasTapped"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:userInfoMutable
options:0
error:&error];
NSLog(@"APP WAS CLOSED DURING PUSH RECEPTION Saved data: %@", jsonData);
lastPush = jsonData;
}
completionHandler(UIBackgroundFetchResultNoData);
}
// [END receive_message]
// [START refresh_token]
- (void)tokenRefreshNotification:(NSNotification *)notification
{
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to appliation server.
}
// [END refresh_token]
// [START connect_to_fcm]
- (void)connectToFcm
{
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
[[FIRMessaging messaging] subscribeToTopic:@"/topics/ios"];
[[FIRMessaging messaging] subscribeToTopic:@"/topics/all"];
}
}];
}
// [END connect_to_fcm]
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"app become active");
[FCMPlugin.fcmPlugin appEnterForeground];
[self connectToFcm];
}
// [START disconnect_from_fcm]
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"app entered background");
[[FIRMessaging messaging] disconnect];
[FCMPlugin.fcmPlugin appEnterBackground];
NSLog(@"Disconnected from FCM");
}
// [END disconnect_from_fcm]
+(NSData*)getLastPush
{
NSData* returnValue = lastPush;
lastPush = nil;
return returnValue;
}
@end