fix: Solve issue that cause 30 seconds delay to display push notifications
Also apply some refactor to improve code style
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// NotificationService.m
|
||||
// EQNNotificationService
|
||||
//
|
||||
// Created by Luca Beretta on 19/12/18.
|
||||
// Copyright © 2018 Luca Beretta. All rights reserved.
|
||||
// Refactored by Andrea Busi
|
||||
// Copyright © 2020 Earthquake Network. All rights reserved.
|
||||
//
|
||||
|
||||
#import "NotificationService.h"
|
||||
@@ -32,201 +32,215 @@
|
||||
self.bestAttemptContent.title = content.title;
|
||||
self.bestAttemptContent.body = content.body;
|
||||
|
||||
// check for media attachment, example here uses custom payload keys mediaUrl and mediaType
|
||||
// check for required informations
|
||||
NSDictionary *userInfo = request.content.userInfo;
|
||||
NSString *messaggioModificato = @"non modificato";
|
||||
|
||||
if (userInfo == nil) {
|
||||
|
||||
NSString *notificationType = [userInfo objectForKey:@"type"];
|
||||
if (userInfo == nil || notificationType == nil) {
|
||||
[self contentComplete];
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ([userInfo objectForKey:@"type"]) {
|
||||
NSString *stringURL = @"https://www.earthquakenetwork.it/icons/";
|
||||
NSString *messaggioModificato = @"";
|
||||
NSString *stringURL = @"https://www.earthquakenetwork.it/icons/";
|
||||
|
||||
if ([notificationType isEqualToString:@"eqn"]) {
|
||||
self.bestAttemptContent.sound = [UNNotificationSound soundNamed:[EQNAllertaSismica sharedInstance].tonoAllarme];
|
||||
|
||||
if ([[userInfo objectForKey:@"type"] 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 ([[userInfo objectForKey:@"type"] 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 ([[userInfo objectForKey:@"type"] 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 ([[userInfo objectForKey:@"type"] 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"]];
|
||||
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";
|
||||
}
|
||||
|
||||
[self loadAttachmentForUrlString:stringURL
|
||||
completionHandler: ^(UNNotificationAttachment *attachment) {
|
||||
self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
|
||||
|
||||
if (![messaggioModificato isEqualToString:@"non modificato"])
|
||||
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", messaggioModificato];
|
||||
}];
|
||||
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"]];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)setMessage:(NSString *)message
|
||||
{
|
||||
BOOL warning = false;
|
||||
BOOL watch = false;
|
||||
BOOL threat = false;
|
||||
BOOL threatFinal = false;
|
||||
BOOL cancellation = false;
|
||||
BOOL information = false;
|
||||
BOOL supplement = false;
|
||||
|
||||
NSString * _message;
|
||||
|
||||
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;
|
||||
|
||||
if (warning) {
|
||||
if (supplement)
|
||||
_message = NSLocalizedString(@"Supplemento di allerta", @"");
|
||||
else if(cancellation)
|
||||
_message = NSLocalizedString(@"Allerta cancellata", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Allerta Tsunami", @"");
|
||||
} else if (watch) {
|
||||
if (supplement)
|
||||
_message = NSLocalizedString(@"Supplemento di valutazione", @"");
|
||||
else if(cancellation)
|
||||
_message = NSLocalizedString(@"Valutazione cancellata", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Valutazione Tsunami", @"");
|
||||
} else if (threat) {
|
||||
if (threatFinal)
|
||||
_message = NSLocalizedString(@"Pericolo cessato", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Pericolo tsunami", @"");
|
||||
} else if (information){
|
||||
_message = NSLocalizedString(@"Messaggio informativo", @"");
|
||||
} else if (supplement){
|
||||
_message = NSLocalizedString(@"Messaggio supplementare", @"");
|
||||
}
|
||||
return _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)loadAttachmentForUrlString:(NSString *)urlString
|
||||
completionHandler:(void (^)(UNNotificationAttachment *))completionHandler
|
||||
- (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];
|
||||
@@ -237,15 +251,11 @@
|
||||
|
||||
NSURLSessionDownloadTask *task = [self.session downloadTaskWithURL:attachmentURL
|
||||
completionHandler: ^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
|
||||
if (error != nil)
|
||||
{
|
||||
if (error != nil) {
|
||||
NSLog(@"%@", error.localizedDescription);
|
||||
|
||||
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@ [error], %@", self.bestAttemptContent.body,error.localizedDescription];
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
} else {
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSURL *localURL = [NSURL fileURLWithPath:[temporaryFileLocation.path
|
||||
stringByAppendingString:fileExt]];
|
||||
@@ -258,8 +268,7 @@
|
||||
URL:localURL
|
||||
options:nil
|
||||
error:&attachmentError];
|
||||
if (attachmentError)
|
||||
{
|
||||
if (attachmentError) {
|
||||
NSLog(@"%@", attachmentError.localizedDescription);
|
||||
|
||||
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@ [attachmentError], %@", self.bestAttemptContent.body,error.localizedDescription];
|
||||
@@ -271,10 +280,4 @@
|
||||
[task resume];
|
||||
}
|
||||
|
||||
- (void)contentComplete
|
||||
{
|
||||
[self.session invalidateAndCancel];
|
||||
self.contentHandler(self.bestAttemptContent);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user