254 lines
11 KiB
Objective-C
254 lines
11 KiB
Objective-C
//
|
|
// AppDelegate.m
|
|
// Earthquake Network
|
|
//
|
|
// Created by Luca Beretta on 12/09/18.
|
|
// Copyright © 2018 Luca Beretta. All rights reserved.
|
|
//
|
|
|
|
#import "AppDelegate.h"
|
|
#import "Costanti.h"
|
|
#import "ServerRequest.h"
|
|
#import "EQNGeneratoreURLServer.h"
|
|
#import "EQNUser.h"
|
|
#import "EQMAccelerometroManager.h"
|
|
#import "EQNManager.h"
|
|
#import "EQNUtility.h"
|
|
#import "EQNAllertaSismica.h"
|
|
#import "EQNAllertaSismica.h"
|
|
#import "EQNNotificheSegnalazioniUtente.h"
|
|
#import "EQNNotificheReteSismiche.h"
|
|
#import "EQNMainTabBarController.h"
|
|
|
|
@import GoogleMaps;
|
|
@import Firebase;
|
|
@import UserNotifications;
|
|
@import GoogleMobileAds;
|
|
|
|
@interface AppDelegate () <FIRMessagingDelegate, UNUserNotificationCenterDelegate>
|
|
@property (strong, nonatomic) NSArray<id<EQNCommandProtocol>> *commands;
|
|
@end
|
|
|
|
@implementation AppDelegate
|
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
|
{
|
|
// start 3rd party SDKs and custom managers
|
|
[GMSServices provideAPIKey:GOOGLE_MAP_KEY];
|
|
[GADMobileAds.sharedInstance startWithCompletionHandler:^(GADInitializationStatus *status) {
|
|
NSLog(@"[AppDelegate] GADMobileAds started with status: %@", status);
|
|
}];
|
|
|
|
[EQNUser defaultUser];
|
|
[EQNManager defaultManager];
|
|
[FIRApp configure];
|
|
[FIRMessaging messaging].delegate = self;
|
|
|
|
// execute commands
|
|
EQNStartupCommandsBuilder *builder = [[EQNStartupCommandsBuilder alloc] init];
|
|
self.commands = [builder build];
|
|
[builder executeWithCommands:self.commands];
|
|
|
|
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
|
|
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
|
|
[[UNUserNotificationCenter currentNotificationCenter]
|
|
requestAuthorizationWithOptions:authOptions
|
|
completionHandler:^(BOOL granted, NSError * _Nullable error) {
|
|
[self registraNotifica];
|
|
}];
|
|
|
|
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
|
|
[application registerForRemoteNotifications];
|
|
|
|
return YES;
|
|
}
|
|
|
|
-(void)registraNotifica{
|
|
|
|
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"notifica_con_mappa" actions:@[] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
|
|
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject: category]];
|
|
}
|
|
|
|
- (void)applicationWillResignActive:(UIApplication *)application {
|
|
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
|
|
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
|
|
}
|
|
|
|
|
|
- (void)applicationDidEnterBackground:(UIApplication *)application {
|
|
// 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.
|
|
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
|
|
[[EQNManager defaultManager] avviaManager];
|
|
[[EQMAccelerometroManager sharedInstance] startUpdatingLocationBackground];
|
|
|
|
NSUInteger counter = [[NSUserDefaults standardUserDefaults] integerForKey:CONTEGGIO_APERTURE_PER_SCONTO];
|
|
counter += 1;
|
|
[[NSUserDefaults standardUserDefaults] setInteger:counter forKey:CONTEGGIO_APERTURE_PER_SCONTO];
|
|
}
|
|
|
|
|
|
- (void)applicationWillEnterForeground:(UIApplication *)application {
|
|
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
|
|
[[EQNManager defaultManager] stopManager];
|
|
[[EQMAccelerometroManager sharedInstance] stopUpdatingLocation];
|
|
|
|
}
|
|
|
|
|
|
- (void)applicationDidBecomeActive:(UIApplication *)application {
|
|
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
|
|
|
|
}
|
|
|
|
|
|
- (void)applicationWillTerminate:(UIApplication *)application {
|
|
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
|
// Saves changes in the application's managed object context before the application terminates.
|
|
}
|
|
|
|
- (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
|
|
/*
|
|
NSString* jsonString = userInfo[@"gcm.notification.userInfo"];
|
|
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
|
|
NSError *jsonError;
|
|
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
options:NSJSONReadingAllowFragments
|
|
error:&jsonError];
|
|
|
|
if ([JSON[@"type"] isEqualToString:@"eqn"]) {
|
|
NSDate *dataRicezione = [NSDate date];
|
|
[[NSUserDefaults standardUserDefaults] setObject:dataRicezione forKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA withDict:JSON];
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
}
|
|
|
|
[self.reteSmartphone fetchNewDataWithCompletionHandler:^(UIBackgroundFetchResult result) {
|
|
completionHandler(result);
|
|
}];*/
|
|
|
|
|
|
completionHandler(UIBackgroundFetchResultNewData);
|
|
}
|
|
|
|
////////////// APPLICAZIONE IN PRIMO PIANO //////////////////////
|
|
|
|
// Receive displayed notifications for iOS 10 devices.
|
|
// Handle incoming notification messages while app is in the foreground.
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
willPresentNotification:(UNNotification *)notification
|
|
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
|
|
NSDictionary *userInfo = notification.request.content.userInfo;
|
|
|
|
NSIndexPath *indexPath;
|
|
if ([userInfo[@"type"] isEqualToString:@"eqn"]) {
|
|
NSDate *dataRicezione = [NSDate date];
|
|
[[NSUserDefaults standardUserDefaults] setObject:dataRicezione forKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
|
|
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA withDict:userInfo];
|
|
indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
}
|
|
else if([userInfo[@"type"] isEqualToString:@"manual"])
|
|
indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
|
|
else if([userInfo[@"type"] isEqualToString:@"official"])
|
|
indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
|
|
else if([userInfo[@"type"] isEqualToString:@"tsunami"])
|
|
indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
|
|
|
|
[self.mainTabBarController sincronizza];
|
|
|
|
|
|
// Change this to your preferred presentation option
|
|
completionHandler(UNNotificationPresentationOptionAlert);
|
|
}
|
|
|
|
|
|
////////////// DOPO AVER CLICCATO SULLA NOTIFICA //////////////////////
|
|
// Handle notification messages after display notification is tapped by the user.
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
|
withCompletionHandler:(void(^)(void))completionHandler {
|
|
|
|
// response.notification.request.content.sound = [UNNotificationSound soundNamed:[EQNAllertaSismica sharedInstance].tonoAllarme];
|
|
NSDictionary *userInfo = response.notification.request.content.userInfo;
|
|
EQNTabBarSection section = EQNTabBarSectionAllerte;
|
|
if ([userInfo[@"type"] isEqualToString:@"eqn"]) {
|
|
NSDate *dataRicezione = [NSDate date];
|
|
[[NSUserDefaults standardUserDefaults] setObject:dataRicezione forKey:NOTIFICHE_RETE_SMARTPHONE_DATA_NOTIFICA];
|
|
|
|
[EQNUtility writeArrayWithCustomObjToUserDefaults:NOTIFICHE_RETE_SMARTPHONE_DIZIONARIO_NOTIFICA withDict:userInfo];
|
|
section = EQNTabBarSectionAllerte;
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
} else if([userInfo[@"type"] isEqualToString:@"manual"]) {
|
|
section = EQNTabBarSectionSegnalazioni;
|
|
} else if([userInfo[@"type"] isEqualToString:@"official"]) {
|
|
section = EQNTabBarSectionRetiSismiche;
|
|
} else if([userInfo[@"type"] isEqualToString:@"tsunami"]) {
|
|
// not handled
|
|
}
|
|
|
|
[self.mainTabBarController sincronizza];
|
|
[self.mainTabBarController selectSection:section];
|
|
|
|
// Print full message.
|
|
completionHandler();
|
|
}
|
|
|
|
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
|
|
{
|
|
NSURL *url = [EQNGeneratoreURLServer urlPosizione];
|
|
[[ServerRequest defaultServerConnectionSingleton] inviaInformazioniAlServerWithURL:url richiesta:posizione success:^(id result) {
|
|
completionHandler(UIBackgroundFetchResultNewData);
|
|
} failure:^(NSError *error) {
|
|
completionHandler(UIBackgroundFetchResultFailed);
|
|
}];
|
|
}
|
|
|
|
|
|
#pragma mark - FIRMessagingDelegate
|
|
|
|
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
|
|
|
|
// Notify about received token.
|
|
// NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
|
|
// [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICA_TOKEN object:nil userInfo:dataDict];
|
|
|
|
NSLog(@"fcmToken %@", fcmToken);
|
|
if (![[NSUserDefaults standardUserDefaults] objectForKey:TOKEN_USER]) {
|
|
|
|
[EQNAllertaSismica sharedInstance].isAbilitato = YES;
|
|
[EQNAllertaSismica sharedInstance].raggioSismiLievi = @"300";
|
|
[EQNAllertaSismica sharedInstance].raggioSismiForti = @"600";
|
|
[EQNAllertaSismica sharedInstance].sismiDaNotificare = @"0";
|
|
[[EQNAllertaSismica sharedInstance] saveUserInfo];
|
|
|
|
[EQNNotificheSegnalazioniUtente sharedInstance].isAbilitato = YES;
|
|
[EQNNotificheSegnalazioniUtente sharedInstance].distanzaPosizione = @"300";
|
|
[[EQNNotificheSegnalazioniUtente sharedInstance] saveUserInfo];
|
|
|
|
[EQNNotificheReteSismiche sharedInstance].isAbilitato = YES;
|
|
[EQNNotificheReteSismiche sharedInstance].distanzaPosizione = @"1000";
|
|
[EQNNotificheReteSismiche sharedInstance].isAbilitaVicini = NO;
|
|
[EQNNotificheReteSismiche sharedInstance].energiaSisma = @"3.5";
|
|
[EQNNotificheReteSismiche sharedInstance].energiaTerremotiForti = @"7.5";
|
|
[[EQNNotificheReteSismiche sharedInstance] saveUserInfo];
|
|
|
|
}
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:fcmToken forKey:TOKEN_USER];
|
|
[EQNUser defaultUser].tokenUser = fcmToken;
|
|
|
|
// TODO: If necessary send token to application server.
|
|
// Note: This callback is fired at each app startup and whenever a new token is generated.
|
|
}
|
|
|
|
|
|
|
|
@end
|