// // 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; @end @implementation NotificationService static NSString * const EQNSoundNotificationEQN = @"alert_sound.wav"; - (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]; 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 *iconName = @""; if ([notificationType isEqualToString:@"eqn"]) { // !!WORKAROUND // this is a workaround to use critical alerts with legacy FCM api // when the server implementation will be migrated to Firebase v1 APIs, this could be removed BOOL isCritical = [[userInfo objectForKey:@"critical"] boolValue]; if (isCritical) { self.bestAttemptContent.sound = [UNNotificationSound criticalSoundNamed:EQNSoundNotificationEQN withAudioVolume:1.0]; } else { self.bestAttemptContent.sound = [UNNotificationSound soundNamed:EQNSoundNotificationEQN]; } NSString *intensity = [userInfo objectForKey:@"intensity"]; switch ([intensity intValue]) { case 0: iconName = @"star_white1.png"; break; case 1: iconName = @"star_lightblue1.png"; break; case 2: iconName = @"star_blue1.png"; break; default: break; } } else if ([notificationType isEqualToString:@"manual"]) { NSString *intensity = [userInfo objectForKey:@"magnitude"]; switch ([intensity intValue]) { case 0: iconName = @"star_green1.png"; break; case 1: iconName = @"star_yellow1.png"; break; case 2: iconName = @"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"]) { iconName = [NSString stringWithFormat:@"star%@2.png", colore]; } else if ([provaider isEqualToString:@"SGC"]) { iconName = [NSString stringWithFormat:@"star3%@2.png", colore]; } else if ([provaider isEqualToString:@"CSN"]) { iconName = [NSString stringWithFormat:@"star3f%@2.png", colore]; } else if ([provaider isEqualToString:@"SSN"]) { iconName = [NSString stringWithFormat:@"star4%@2.png", colore]; } else if ([provaider isEqualToString:@"INPRES"]) { iconName = [NSString stringWithFormat:@"star4r%@2.png", colore]; } else if ([provaider isEqualToString:@"FUNVISIS"]) { iconName = [NSString stringWithFormat:@"star6%@2.png", colore]; } else if ([provaider isEqualToString:@"Ineter"]) { iconName = [NSString stringWithFormat:@"triangle%@2.png", colore]; } else if ([provaider isEqualToString:@"RSN"]) { iconName = [NSString stringWithFormat:@"triangle2%@2.png", colore]; } else if ([provaider isEqualToString:@"PHIVOLCS"]) { iconName = [NSString stringWithFormat:@"triround_inner%@2.png", colore]; } else if ([provaider isEqualToString:@"IGEPN"]) { iconName = [NSString stringWithFormat:@"triround%@2.png", colore]; } else if ([provaider isEqualToString:@"INGV"]) { iconName = [NSString stringWithFormat:@"circle%@2.png", colore]; } else if ([provaider isEqualToString:@"EMSC"]) { iconName = [NSString stringWithFormat:@"dyamond%@2.png", colore]; } else if ([provaider isEqualToString:@"IGP"]) { iconName = [NSString stringWithFormat:@"dyamond_round%@2.png", colore]; } else if ([provaider isEqualToString:@"JMA"]) { iconName = [NSString stringWithFormat:@"esa%@2.png", colore]; } else if ([provaider isEqualToString:@"GEONET"]) { iconName = [NSString stringWithFormat:@"oct%@2.png", colore]; } if ([provaider isEqualToString:@"CSI"]) { iconName = [NSString stringWithFormat:@"penta%@2.png", colore]; } else if ([provaider isEqualToString:@"IGN"]) { iconName = [NSString stringWithFormat:@"square%@2.png", colore]; } else if ([provaider isEqualToString:@"UASD"] || [provaider isEqualToString:@"BDTIM"] || [provaider isEqualToString:@"NCS"]) { iconName = [NSString stringWithFormat:@"thick_star%@2.png", colore]; } else if ([provaider isEqualToString:@"RSPR"]) { iconName = [NSString stringWithFormat:@"star6f%@2.png", colore]; } } if (![iconName isEqualToString:@""]) { iconName = [iconName stringByReplacingOccurrencesOfString:@".png" withString:@""]; NSURL *imageUrl = [NSBundle.mainBundle URLForResource:iconName withExtension:@"png"]; NSError *attachmentError = nil; UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:iconName URL:imageUrl options:nil error:&attachmentError]; if (!attachmentError) { self.bestAttemptContent.attachments = @[attachment]; } } [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]; } - (void)contentComplete { self.contentHandler(self.bestAttemptContent); } @end