65ed82445b
Also apply some refactor to improve code style
284 lines
13 KiB
Objective-C
284 lines
13 KiB
Objective-C
//
|
|
// NotificationService.m
|
|
// EQNNotificationService
|
|
//
|
|
// Refactored by Andrea Busi
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
#import "NotificationService.h"
|
|
#import "EQNAllertaSismica.h"
|
|
@interface NotificationService ()
|
|
|
|
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
|
|
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
|
|
@property (nonatomic, strong) NSURLSession *session;
|
|
@end
|
|
|
|
@implementation NotificationService
|
|
|
|
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler
|
|
{
|
|
self.contentHandler = contentHandler;
|
|
self.bestAttemptContent = [request.content mutableCopy];
|
|
|
|
// Configure the notification's payload.
|
|
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
|
|
content.title = [NSString localizedUserNotificationStringForKey:request.content.title arguments:nil];
|
|
content.body = [NSString localizedUserNotificationStringForKey:request.content.body
|
|
arguments:nil];
|
|
content.sound = [UNNotificationSound defaultSound];
|
|
|
|
self.bestAttemptContent.title = content.title;
|
|
self.bestAttemptContent.body = content.body;
|
|
|
|
// check for required informations
|
|
NSDictionary *userInfo = request.content.userInfo;
|
|
NSString *notificationType = [userInfo objectForKey:@"type"];
|
|
if (userInfo == nil || notificationType == nil) {
|
|
[self contentComplete];
|
|
return;
|
|
}
|
|
|
|
|
|
NSString *messaggioModificato = @"";
|
|
NSString *stringURL = @"https://www.earthquakenetwork.it/icons/";
|
|
|
|
if ([notificationType isEqualToString:@"eqn"]) {
|
|
self.bestAttemptContent.sound = [UNNotificationSound soundNamed:[EQNAllertaSismica sharedInstance].tonoAllarme];
|
|
|
|
NSString *intensity = [userInfo objectForKey:@"intensity"];
|
|
switch ([intensity intValue]) {
|
|
case 0:
|
|
stringURL = [stringURL stringByAppendingString:@"star_white1.png"];
|
|
break;
|
|
case 1:
|
|
stringURL = [stringURL stringByAppendingString:@"star_lightblue1.png"];
|
|
break;
|
|
case 2:
|
|
stringURL = [stringURL stringByAppendingString:@"star_blue1.png"];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else if ([notificationType isEqualToString:@"manual"]) {
|
|
NSString *intensity = [userInfo objectForKey:@"magnitude"];
|
|
switch ([intensity intValue]) {
|
|
case 0:
|
|
stringURL = [stringURL stringByAppendingString:@"star_green1.png"];
|
|
break;
|
|
case 1:
|
|
stringURL = [stringURL stringByAppendingString:@"star_yellow1.png"];
|
|
break;
|
|
case 2:
|
|
stringURL = [stringURL stringByAppendingString:@"star_red1.png"];
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else if ([notificationType isEqualToString:@"official"]) {
|
|
NSString *provaider = [userInfo objectForKey:@"provider"];
|
|
double intensity = [[userInfo objectForKey:@"magnitude"] doubleValue];
|
|
|
|
NSString *colore = @"";
|
|
if (intensity < 2.0) {
|
|
colore = @"_white";
|
|
} else if (intensity < 3.5) {
|
|
colore = @"_green";
|
|
} else if (intensity < 4.5) {
|
|
colore = @"_yellow";
|
|
} else if (intensity < 5.5) {
|
|
colore = @"_red";
|
|
} else {
|
|
colore = @"_purple";
|
|
}
|
|
|
|
if ([provaider isEqualToString:@"USGS"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"SGC"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star3%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"CSN"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star3f%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"SSN"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star4%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"INPRES"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star4r%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"FUNVISIS"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star6%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"Ineter"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triangle%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"RSN"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triangle2%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"PHIVOLCS"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triround_inner%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"IGEPN"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triround%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"INGV"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"circle%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"EMSC"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"dyamond%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"IGP"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"dyamond_round%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"JMA"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"esa%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"GEONET"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"oct%@2.png", colore]];
|
|
} if ([provaider isEqualToString:@"CSI"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"penta%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"IGN"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"square%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"UASD"] || [provaider isEqualToString:@"BDTIM"] || [provaider isEqualToString:@"NCS"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"thick_star%@2.png", colore]];
|
|
} else if ([provaider isEqualToString:@"RSPR"]) {
|
|
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star6f%@2.png", colore]];
|
|
}
|
|
} else if ([notificationType isEqualToString:@"tsunami"]){
|
|
NSString *color = [userInfo objectForKey:@"color"];
|
|
|
|
if ([color isEqualToString:@"gr"])
|
|
stringURL = [stringURL stringByAppendingString:@"tsunami_green.png"];
|
|
if ([color isEqualToString:@"ye"])
|
|
stringURL = [stringURL stringByAppendingString:@"tsunami_yellow.png"];
|
|
if ([color isEqualToString:@"or"])
|
|
stringURL = [stringURL stringByAppendingString:@"tsunami_orange.png"];
|
|
if ([color isEqualToString:@"bl"])
|
|
stringURL = [stringURL stringByAppendingString:@"tsunami_blue.png"];
|
|
if ([color isEqualToString:@"re"])
|
|
stringURL = [stringURL stringByAppendingString:@"tsunami_red.png"];
|
|
|
|
messaggioModificato = [self setMessage:[userInfo objectForKey:@"message"]];
|
|
}
|
|
|
|
[self loadAttachmentForUrlString:stringURL completionHandler:^(UNNotificationAttachment *attachment) {
|
|
self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
|
|
|
|
if (![messaggioModificato isEqualToString:@""]) {
|
|
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", messaggioModificato];
|
|
}
|
|
|
|
[self contentComplete];
|
|
}];
|
|
}
|
|
|
|
- (void)serviceExtensionTimeWillExpire
|
|
{
|
|
// Called just before the extension will be terminated by the system.
|
|
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
|
[self contentComplete];
|
|
self.contentHandler(self.bestAttemptContent);
|
|
}
|
|
|
|
- (void)contentComplete
|
|
{
|
|
[self.session invalidateAndCancel];
|
|
self.contentHandler(self.bestAttemptContent);
|
|
}
|
|
|
|
#pragma mark - Helpers
|
|
|
|
- (NSString *)setMessage:(NSString *)message
|
|
{
|
|
BOOL warning = NO;
|
|
BOOL watch = NO;
|
|
BOOL threat = NO;
|
|
BOOL threatFinal = NO;
|
|
BOOL cancellation = NO;
|
|
BOOL information = NO;
|
|
BOOL supplement = NO;
|
|
|
|
|
|
if ([message.lowercaseString containsString:@"warning"]) {
|
|
warning = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"watch"]) {
|
|
watch = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"threat"]) {
|
|
threat = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"final"]) {
|
|
threatFinal = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"cancellation"]) {
|
|
cancellation = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"information"]) {
|
|
information = YES;
|
|
}
|
|
if ([message.lowercaseString containsString:@"supplement"]) {
|
|
supplement = YES;
|
|
}
|
|
|
|
NSString *localizedMessage = @"";
|
|
if (warning) {
|
|
if (supplement) {
|
|
localizedMessage = NSLocalizedString(@"Supplemento di allerta", @"");
|
|
} else if (cancellation) {
|
|
localizedMessage = NSLocalizedString(@"Allerta cancellata", @"");
|
|
} else {
|
|
localizedMessage = NSLocalizedString(@"Allerta Tsunami", @"");
|
|
}
|
|
} else if (watch) {
|
|
if (supplement) {
|
|
localizedMessage = NSLocalizedString(@"Supplemento di valutazione", @"");
|
|
} else if (cancellation) {
|
|
localizedMessage = NSLocalizedString(@"Valutazione cancellata", @"");
|
|
} else {
|
|
localizedMessage = NSLocalizedString(@"Valutazione Tsunami", @"");
|
|
}
|
|
} else if (threat) {
|
|
if (threatFinal) {
|
|
localizedMessage = NSLocalizedString(@"Pericolo cessato", @"");
|
|
} else {
|
|
localizedMessage = NSLocalizedString(@"Pericolo tsunami", @"");
|
|
}
|
|
} else if (information) {
|
|
localizedMessage = NSLocalizedString(@"Messaggio informativo", @"");
|
|
} else if (supplement) {
|
|
localizedMessage = NSLocalizedString(@"Messaggio supplementare", @"");
|
|
}
|
|
return localizedMessage;
|
|
}
|
|
|
|
- (void)loadAttachmentForUrlString:(NSString *)urlString completionHandler:(void (^)(UNNotificationAttachment *))completionHandler
|
|
{
|
|
__block UNNotificationAttachment *attachment = nil;
|
|
__block NSURL *attachmentURL = [NSURL URLWithString:urlString];
|
|
|
|
NSString *fileExt = [@"." stringByAppendingString:[urlString pathExtension]];
|
|
|
|
self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
|
|
|
|
NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:attachmentURL
|
|
completionHandler: ^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
|
|
if (error != nil) {
|
|
NSLog(@"%@", error.localizedDescription);
|
|
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@ [error], %@", self.bestAttemptContent.body,error.localizedDescription];
|
|
|
|
} else {
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path
|
|
stringByAppendingString:fileExt]];
|
|
[fileManager moveItemAtURL:temporaryFileLocation
|
|
toURL:localURL
|
|
error:&error];
|
|
|
|
NSError *attachmentError = nil;
|
|
attachment = [UNNotificationAttachment attachmentWithIdentifier:[attachmentURL lastPathComponent]
|
|
URL:localURL
|
|
options:nil
|
|
error:&attachmentError];
|
|
if (attachmentError) {
|
|
NSLog(@"%@", attachmentError.localizedDescription);
|
|
|
|
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@ [attachmentError], %@", self.bestAttemptContent.body,error.localizedDescription];
|
|
}
|
|
}
|
|
completionHandler(attachment);
|
|
}];
|
|
|
|
[task resume];
|
|
}
|
|
|
|
@end
|