2
2
3
3
#import < AVFoundation/AVFoundation.h>
4
4
#import < Photos/Photos.h>
5
- #import < PushKit/PushKit.h>
6
5
#import < UserNotifications/UserNotifications.h>
7
6
8
7
#import " LTHPasscodeViewController.h"
@@ -56,7 +55,7 @@ typedef NS_ENUM(NSUInteger, URLType) {
56
55
URLTypeRecoverLink
57
56
};
58
57
59
- @interface AppDelegate () <UIAlertViewDelegate, PKPushRegistryDelegate, UNUserNotificationCenterDelegate , LTHPasscodeViewControllerDelegate> {
58
+ @interface AppDelegate () <UIAlertViewDelegate, UNUserNotificationCenterDelegate , LTHPasscodeViewControllerDelegate> {
60
59
BOOL isAccountFirstLogin;
61
60
BOOL isFetchNodesDone;
62
61
@@ -202,8 +201,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
202
201
isFetchNodesDone = NO ;
203
202
204
203
if (sessionV3) {
205
- [self registerForVoIPNotifications ];
206
- [self registerForLocalNotifications ];
204
+ [self registerForNotifications ];
207
205
isAccountFirstLogin = NO ;
208
206
if ([[NSUserDefaults standardUserDefaults ] objectForKey: @" IsChatEnabled" ] == nil ) {
209
207
[[NSUserDefaults standardUserDefaults ] setBool: YES forKey: @" IsChatEnabled" ];
@@ -279,7 +277,12 @@ - (void)applicationWillResignActive:(UIApplication *)application {
279
277
- (void )applicationDidEnterBackground : (UIApplication *)application {
280
278
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
281
279
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
282
- [self startBackgroundTask ];
280
+ [[MEGASdkManager sharedMEGAChatSdk ] setBackgroundStatus: YES ];
281
+
282
+ BOOL pendingTransfers = [[[[MEGASdkManager sharedMEGASdk ] transfers ] size ] integerValue ] > 0 || [[[[MEGASdkManager sharedMEGASdkFolder ] transfers ] size ] integerValue ] > 0 ;
283
+ if (pendingTransfers) {
284
+ [self startBackgroundTask ];
285
+ }
283
286
284
287
if (self.privacyView == nil ) {
285
288
UIViewController *privacyVC = [[UIStoryboard storyboardWithName: @" Launch" bundle: nil ] instantiateViewControllerWithIdentifier: @" PrivacyViewControllerID" ];
@@ -296,6 +299,8 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {
296
299
}
297
300
298
301
- (void )applicationWillEnterForeground : (UIApplication *)application {
302
+ [[MEGASdkManager sharedMEGAChatSdk ] setBackgroundStatus: NO ];
303
+
299
304
if ([[MEGASdkManager sharedMEGASdk ] isLoggedIn ] && [[CameraUploads syncManager ] isCameraUploadsEnabled ]) {
300
305
MEGALogInfo (@" Enable Camera Uploads" );
301
306
[[CameraUploads syncManager ] setIsCameraUploadsEnabled: YES ];
@@ -308,6 +313,8 @@ - (void)applicationWillEnterForeground:(UIApplication *)application {
308
313
window.frame = [[UIScreen mainScreen ] bounds ];
309
314
}
310
315
}
316
+
317
+ [[MEGASdkManager sharedMEGAChatSdk ] retryPendingConnections ];
311
318
}
312
319
313
320
- (void )applicationDidBecomeActive : (UIApplication *)application {
@@ -374,6 +381,34 @@ - (void)applicationProtectedDataWillBecomeUnavailable:(UIApplication *)applicati
374
381
}
375
382
}
376
383
384
+ - (void )application : (UIApplication *)application didRegisterUserNotificationSettings : (UIUserNotificationSettings *)notificationSettings {
385
+ [application registerForRemoteNotifications ];
386
+ }
387
+
388
+ - (void )application : (UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken : (NSData *)deviceToken {
389
+ if ([deviceToken length ] == 0 ) {
390
+ MEGALogError (@" Token length is 0" );
391
+ return ;
392
+ }
393
+
394
+ const unsigned char *dataBuffer = (const unsigned char *)deviceToken.bytes ;
395
+
396
+ NSUInteger dataLength = deviceToken.length ;
397
+ NSMutableString *hexString = [NSMutableString stringWithCapacity: (dataLength * 2 )];
398
+
399
+ for (int i = 0 ; i < dataLength; ++i) {
400
+ [hexString appendString: [NSString stringWithFormat: @" %02lx " , (unsigned long )dataBuffer[i]]];
401
+ }
402
+
403
+ NSString *deviceTokenString = [NSString stringWithString: hexString];
404
+ MEGALogDebug (@" Device token %@ " , deviceTokenString);
405
+ [[MEGASdkManager sharedMEGASdk ] registeriOSdeviceToken: deviceTokenString];
406
+ }
407
+
408
+ - (void )application : (UIApplication *)application didFailToRegisterForRemoteNotificationsWithError : (NSError *)error {
409
+ MEGALogError (@" Failed to register for remote notifications %@ " , error);
410
+ }
411
+
377
412
#pragma mark - Private
378
413
379
414
- (void )setupAppearance {
@@ -938,28 +973,21 @@ - (void)showMainTabBar {
938
973
939
974
[[CameraUploads syncManager ] setTabBarController: _mainTBC];
940
975
if (isAccountFirstLogin) {
941
- [self registerForVoIPNotifications ];
942
- [self registerForLocalNotifications ];
976
+ [self registerForNotifications ];
943
977
}
944
978
}
945
979
946
- - (void )registerForVoIPNotifications {
947
- dispatch_queue_t mainQueue = dispatch_get_main_queue ();
948
- PKPushRegistry *voipRegistry = [[PKPushRegistry alloc ] initWithQueue: mainQueue];
949
- voipRegistry.delegate = self;
950
- voipRegistry.desiredPushTypes = [NSSet setWithObject: PKPushTypeVoIP];
951
- }
952
-
953
-
954
-
955
- - (void )registerForLocalNotifications {
956
- if (NSClassFromString (@" UNUserNotificationCenter" )) {
980
+ - (void )registerForNotifications {
981
+ if ([[UIDevice currentDevice ] systemVersionGreaterThanOrEqualVersion: @" 10.0" ]) {
957
982
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter ];
958
983
center.delegate = self;
959
984
[center requestAuthorizationWithOptions: (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert )
960
985
completionHandler: ^(BOOL granted, NSError * _Nullable error) {
961
986
if (!error) {
962
- NSLog (@" request authorization succeeded!" );
987
+ MEGALogInfo (@" Request notifications authorization succeeded" );
988
+ }
989
+ if (granted) {
990
+ [self notificationsSettings ];
963
991
}
964
992
}];
965
993
} else {
@@ -969,6 +997,16 @@ - (void)registerForLocalNotifications {
969
997
}
970
998
}
971
999
1000
+ - (void )notificationsSettings {
1001
+ UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter ];
1002
+ [center getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings *settings) {
1003
+ MEGALogInfo (@" Notifications settings %@ " , settings);
1004
+ if (settings.authorizationStatus == UNAuthorizationStatusAuthorized ) {
1005
+ [[UIApplication sharedApplication ] registerForRemoteNotifications ];
1006
+ }
1007
+ }];
1008
+ }
1009
+
972
1010
#pragma mark - Battery changed
973
1011
974
1012
- (void )batteryChanged : (NSNotification *)notification {
@@ -1165,79 +1203,11 @@ - (void)setSystemLanguage {
1165
1203
}
1166
1204
}
1167
1205
1168
- - (void )setDefaultLanguage {
1206
+ - (void )setDefaultLanguage {
1207
+ [[MEGASdkManager sharedMEGASdk ] setLanguageCode: @" en" ];
1169
1208
[[LocalizationSystem sharedLocalSystem ] setLanguage: @" en" ];
1170
1209
}
1171
1210
1172
- #pragma mark - PKPushRegistryDelegate
1173
-
1174
- - (void )pushRegistry : (PKPushRegistry *)registry didUpdatePushCredentials : (PKPushCredentials *)credentials forType : (NSString *)type {
1175
- if ([credentials.token length ] == 0 ) {
1176
- MEGALogError (@" VoIP token length is 0" );
1177
- return ;
1178
- }
1179
-
1180
- const unsigned char *dataBuffer = (const unsigned char *)credentials.token .bytes ;
1181
-
1182
- NSUInteger dataLength = credentials.token .length ;
1183
- NSMutableString *hexString = [NSMutableString stringWithCapacity: (dataLength * 2 )];
1184
-
1185
- for (int i = 0 ; i < dataLength; ++i) {
1186
- [hexString appendString: [NSString stringWithFormat: @" %02lx " , (unsigned long )dataBuffer[i]]];
1187
- }
1188
-
1189
- NSString *deviceTokenString = [NSString stringWithString: hexString];
1190
- MEGALogDebug (@" Device token %@ " , deviceTokenString);
1191
- [[MEGASdkManager sharedMEGASdk ] registeriOSdeviceToken: deviceTokenString];
1192
- }
1193
-
1194
- - (void )pushRegistry : (PKPushRegistry *)registry didReceiveIncomingPushWithPayload : (PKPushPayload *)payload forType : (NSString *)type {
1195
- MEGALogDebug (@" didReceiveIncomingPushWithPayload: %@ " , [payload dictionaryPayload ]);
1196
-
1197
- NSInteger megatype = [[[payload dictionaryPayload ] objectForKey: @" megatype" ] integerValue ];
1198
- NSString *body = nil ;
1199
-
1200
- switch (megatype) {
1201
- case 1 :
1202
- body = AMLocalizedString (@" newSharedFolder" , @" Notification text body shown when you have received a new shared folder" );
1203
- break ;
1204
- case 2 :
1205
- body = AMLocalizedString (@" newMessage" , @" Notification text body shown when you have received a new chat message" );
1206
- break ;
1207
- case 3 :
1208
- body = AMLocalizedString (@" contactRequest" , @" Notification text body shown when you have received a contact request" );
1209
- break ;
1210
-
1211
- default :
1212
- break ;
1213
- }
1214
-
1215
- if (NSClassFromString (@" UNMutableNotificationContent" )) {
1216
- UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc ] init ];
1217
-
1218
- content.body = body;
1219
- content.sound = [UNNotificationSound defaultSound ];
1220
-
1221
- NSString *identifier = [NSString stringWithFormat: @" %@ " , [payload dictionaryPayload ]];
1222
- UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier: identifier
1223
- content: content
1224
- trigger: nil ];
1225
-
1226
- UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter ];
1227
- [center addNotificationRequest: request withCompletionHandler: ^(NSError * _Nullable error) {
1228
- if (error) {
1229
- MEGALogError (@" Add NotificationRequest failed with error: %@ " , error);
1230
- }
1231
- }];
1232
- } else if ([UIApplication sharedApplication ].applicationState != UIApplicationStateActive) {
1233
- UILocalNotification *localNotification = [[UILocalNotification alloc ] init ];
1234
- localNotification.alertBody = body;
1235
- localNotification.soundName = UILocalNotificationDefaultSoundName;
1236
-
1237
- [[UIApplication sharedApplication ] presentLocalNotificationNow: localNotification];
1238
- }
1239
- }
1240
-
1241
1211
#pragma mark - MEGAGlobalDelegate
1242
1212
1243
1213
- (void )onUsersUpdate : (MEGASdk *)api userList : (MEGAUserList *)userList {
0 commit comments