// // NotificationService.m // EQNNotificationService // // Created by Luca Beretta on 19/12/18. // Copyright © 2018 Luca Beretta. 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]; // Modify the notification content here... self.bestAttemptContent.title = [NSString stringWithFormat:@"%@ [modified]", self.bestAttemptContent.title]; self.contentHandler(self.bestAttemptContent); } */ - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // Modify the notification content here... // 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; /* // Deliver the notification in five seconds. UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" content:content trigger:trigger]; // Schedule the notification. UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request]; */ // check for media attachment, example here uses custom payload keys mediaUrl and mediaType NSDictionary *userInfo = request.content.userInfo; NSString *messaggioModificato = @"non modificato"; if (userInfo == nil) { [self contentComplete]; return; } if ([userInfo objectForKey:@"type"]) { NSString *stringURL = @"http://www.earthquakenetwork.it/icons/"; 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 = @"green"; 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]]; if ([provaider isEqualToString:@"SGC"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star3%@2.png", colore]]; if ([provaider isEqualToString:@"CSN"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star3f%@2.png", colore]]; if ([provaider isEqualToString:@"SSN"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star4%@2.png", colore]]; if ([provaider isEqualToString:@"INPRES"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star4r%@2.png", colore]]; if ([provaider isEqualToString:@"FUNVISIS"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star6%@2.png", colore]]; if ([provaider isEqualToString:@"Ineter"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triangle%@2.png", colore]]; if ([provaider isEqualToString:@"RSN"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triangle2%@2.png", colore]]; if ([provaider isEqualToString:@"PHIVOLCS"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triround_inner%@2.png", colore]]; if ([provaider isEqualToString:@"IGEPN"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"triround%@2.png", colore]]; if ([provaider isEqualToString:@"INGV"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"circle%@2.png", colore]]; if ([provaider isEqualToString:@"EMSC"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"dyamond%@2.png", colore]]; if ([provaider isEqualToString:@"IGP"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"dyamond_round%@2.png", colore]]; if ([provaider isEqualToString:@"JMA"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"esa%@2.png", colore]]; 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]]; if ([provaider isEqualToString:@"IGN"]) stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"square%@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"]]; } [self loadAttachmentForUrlString:stringURL completionHandler: ^(UNNotificationAttachment *attachment) { self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil]; if (![messaggioModificato isEqualToString:@"non modificato"]) self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", messaggioModificato]; }]; } } -(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; } - (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 { __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]; } - (void)contentComplete { [self.session invalidateAndCancel]; self.contentHandler(self.bestAttemptContent); } @end