forked from arnesson/cordova-plugin-firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirebasePlugin.m
266 lines (217 loc) · 10.8 KB
/
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#import "FirebasePlugin.h"
#import <Cordova/CDV.h>
#import "AppDelegate.h"
#import "Firebase.h"
@import FirebaseInstanceID;
@import FirebaseMessaging;
@import FirebaseAnalytics;
@import FirebaseRemoteConfig;
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif
@implementation FirebasePlugin
@synthesize notificationCallbackId;
@synthesize tokenRefreshCallbackId;
@synthesize notificationStack;
static NSInteger const kNotificationStackSize = 10;
static FirebasePlugin *firebasePlugin;
+ (FirebasePlugin *) firebasePlugin {
return firebasePlugin;
}
- (void)pluginInitialize {
NSLog(@"Starting Firebase plugin");
firebasePlugin = self;
}
// DEPRECATED - alias of getToken
- (void)getInstanceId:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:
[[FIRInstanceID instanceID] token]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)getToken:(CDVInvokedUrlCommand *)command {
CDVPluginResult *pluginResult;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:
[[FIRInstanceID instanceID] token]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)hasPermission:(CDVInvokedUrlCommand *)command
{
BOOL enabled = NO;
UIApplication *application = [UIApplication sharedApplication];
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
enabled = application.currentUserNotificationSettings.types != UIUserNotificationTypeNone;
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
enabled = application.enabledRemoteNotificationTypes != UIRemoteNotificationTypeNone;
#pragma GCC diagnostic pop
}
NSMutableDictionary* message = [NSMutableDictionary dictionaryWithCapacity:1];
[message setObject:[NSNumber numberWithBool:enabled] forKey:@"isEnabled"];
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:message];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}
- (void)grantPermission:(CDVInvokedUrlCommand *)command {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
if ([[UIApplication sharedApplication]respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType notificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
#pragma GCC diagnostic pop
}
} else {
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// IOS 10
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}
];
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
[[FIRMessaging messaging] setRemoteMessageDelegate:self];
#endif
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)setBadgeNumber:(CDVInvokedUrlCommand *)command {
int number = [[command.arguments objectAtIndex:0] intValue];
[self.commandDelegate runInBackground:^{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:number];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getBadgeNumber:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
long badge = [[UIApplication sharedApplication] applicationIconBadgeNumber];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDouble:badge];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)subscribe:(CDVInvokedUrlCommand *)command {
NSString* topic = [NSString stringWithFormat:@"/topics/%@", [command.arguments objectAtIndex:0]];
[[FIRMessaging messaging] subscribeToTopic: topic];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)unsubscribe:(CDVInvokedUrlCommand *)command {
NSString* topic = [NSString stringWithFormat:@"/topics/%@", [command.arguments objectAtIndex:0]];
[[FIRMessaging messaging] unsubscribeFromTopic: topic];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
- (void)onNotificationOpen:(CDVInvokedUrlCommand *)command {
self.notificationCallbackId = command.callbackId;
if (self.notificationStack != nil && [self.notificationStack count]) {
for (NSDictionary *userInfo in self.notificationStack) {
[self sendNotification:userInfo];
}
[self.notificationStack removeAllObjects];
}
}
- (void)onTokenRefresh:(CDVInvokedUrlCommand *)command {
self.tokenRefreshCallbackId = command.callbackId;
NSString* currentToken = [[FIRInstanceID instanceID] token];
if (currentToken != nil) {
[self sendToken:currentToken];
}
}
- (void)sendNotification:(NSDictionary *)userInfo {
if (self.notificationCallbackId != nil) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:userInfo];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.notificationCallbackId];
} else {
if (!self.notificationStack) {
self.notificationStack = [[NSMutableArray alloc] init];
}
// stack notifications until a callback has been registered
[self.notificationStack addObject:userInfo];
if ([self.notificationStack count] >= kNotificationStackSize) {
[self.notificationStack removeLastObject];
}
}
}
- (void)sendToken:(NSString *)token {
if (self.tokenRefreshCallbackId != nil) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:token];
[pluginResult setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.tokenRefreshCallbackId];
}
}
- (void)logEvent:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* name = [command.arguments objectAtIndex:0];
NSDictionary* parameters = [command.arguments objectAtIndex:1];
[FIRAnalytics logEventWithName:name parameters:parameters];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserId:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* id = [command.arguments objectAtIndex:0];
[FIRAnalytics setUserID:id];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)setUserProperty:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* name = [command.arguments objectAtIndex:0];
NSString* value = [command.arguments objectAtIndex:1];
[FIRAnalytics setUserPropertyString:value forName:name];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)fetch:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
[remoteConfig fetchWithCompletionHandler:^(FIRRemoteConfigFetchStatus status, NSError * _Nullable error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
}];
}];
}
- (void)activateFetched:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
BOOL activated = [remoteConfig activateFetched];
CDVPluginResult *pluginResult;
if (activated) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
- (void)getValue:(CDVInvokedUrlCommand *)command {
[self.commandDelegate runInBackground:^{
NSString* key = [command.arguments objectAtIndex:0];
FIRRemoteConfig* remoteConfig = [FIRRemoteConfig remoteConfig];
NSString* value = remoteConfig[key].stringValue;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:value];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}];
}
@end