forked from arnesson/cordova-plugin-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate+FirebasePlugin.m
executable file
·217 lines (165 loc) · 8.31 KB
/
AppDelegate+FirebasePlugin.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
#import "AppDelegate+FirebasePlugin.h"
#import "FirebasePlugin.h"
#import "Firebase.h"
#import <objc/runtime.h>
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
// running iOS 10 and above. Implement FIRMessagingDelegate to receive data message via FCM for
// devices running iOS 10 and above.
@interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
@end
#endif
#define kApplicationInBackgroundKey @"applicationInBackground"
#define kDelegateKey @"delegate"
@implementation AppDelegate (FirebasePlugin)
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)setDelegate:(id)delegate {
objc_setAssociatedObject(self, kDelegateKey, delegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (id)delegate {
return objc_getAssociatedObject(self, kDelegateKey);
}
#endif
+ (void)load {
Method original = class_getInstanceMethod(self, @selector(application:didFinishLaunchingWithOptions:));
Method swizzled = class_getInstanceMethod(self, @selector(application:swizzledDidFinishLaunchingWithOptions:));
method_exchangeImplementations(original, swizzled);
}
- (void)setApplicationInBackground:(NSNumber *)applicationInBackground {
objc_setAssociatedObject(self, kApplicationInBackgroundKey, applicationInBackground, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)applicationInBackground {
return objc_getAssociatedObject(self, kApplicationInBackgroundKey);
}
- (BOOL)application:(UIApplication *)application swizzledDidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self application:application swizzledDidFinishLaunchingWithOptions:launchOptions];
// get GoogleService-Info.plist file path
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
// if file is successfully found, use it
if(filePath){
NSLog(@"GoogleService-Info.plist found, setup: [FIRApp configureWithOptions]");
// create firebase configure options passing .plist as content
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
// configure FIRApp with options
[FIRApp configureWithOptions:options];
}
// no .plist found, try default App
if (![FIRApp defaultApp] && !filePath) {
NSLog(@"GoogleService-Info.plist NOT FOUND, setup: [FIRApp defaultApp]");
[FIRApp configure];
}
// [START set_messaging_delegate]
[FIRMessaging messaging].delegate = self;
// [END set_messaging_delegate]
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
self.delegate = [UNUserNotificationCenter currentNotificationCenter].delegate;
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:)
name:kFIRInstanceIDTokenRefreshNotification object:nil];
self.applicationInBackground = @(YES);
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self connectToFcm];
self.applicationInBackground = @(NO);
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
self.applicationInBackground = @(YES);
NSLog(@"Disconnected from FCM");
}
- (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];
[FirebasePlugin.firebasePlugin sendToken:refreshedToken];
}
- (void)connectToFcm {
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);
}
}];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[FIRMessaging messaging].APNSToken = deviceToken;
NSLog(@"deviceToken1 = %@", deviceToken);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSDictionary *mutableUserInfo = [userInfo mutableCopy];
[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
// Print full message.
NSLog(@"%@", mutableUserInfo);
[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
NSDictionary *mutableUserInfo = [userInfo mutableCopy];
[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
// Print full message.
NSLog(@"%@", mutableUserInfo);
completionHandler(UIBackgroundFetchResultNewData);
[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
}
// [START ios_10_data_message]
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
// To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES.
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
NSLog(@"Received data message: %@", remoteMessage.appData);
// This will allow us to handle FCM data-only push messages even if the permission for push
// notifications is yet missing. This will only work when the app is in the foreground.
[FirebasePlugin.firebasePlugin sendNotification:remoteMessage.appData];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Unable to register for remote notifications: %@", error);
}
// [END ios_10_data_message]
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
[self.delegate userNotificationCenter:center
willPresentNotification:notification
withCompletionHandler:completionHandler];
if (![notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
return;
NSDictionary *mutableUserInfo = [notification.request.content.userInfo mutableCopy];
[mutableUserInfo setValue:self.applicationInBackground forKey:@"tap"];
// Print full message.
NSLog(@"%@", mutableUserInfo);
completionHandler(UNNotificationPresentationOptionAlert);
[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
}
- (void) userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[self.delegate userNotificationCenter:center
didReceiveNotificationResponse:response
withCompletionHandler:completionHandler];
if (![response.notification.request.trigger isKindOfClass:UNPushNotificationTrigger.class])
return;
NSDictionary *mutableUserInfo = [response.notification.request.content.userInfo mutableCopy];
[mutableUserInfo setValue:@YES forKey:@"tap"];
// Print full message.
NSLog(@"Response %@", mutableUserInfo);
[FirebasePlugin.firebasePlugin sendNotification:mutableUserInfo];
completionHandler();
}
// Receive data message on iOS 10 devices.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", [remoteMessage appData]);
}
#endif
@end