refactor: Improve code style
This commit is contained in:
@@ -17,67 +17,37 @@
|
||||
|
||||
@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];
|
||||
// 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)
|
||||
{
|
||||
if (userInfo == nil) {
|
||||
|
||||
[self contentComplete];
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ([userInfo objectForKey:@"type"])
|
||||
{
|
||||
|
||||
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]) {
|
||||
@@ -93,11 +63,7 @@
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else if ([[userInfo objectForKey:@"type"] isEqualToString:@"manual"]) {
|
||||
|
||||
|
||||
} else if ([[userInfo objectForKey:@"type"] isEqualToString:@"manual"]) {
|
||||
NSString *intensity = [userInfo objectForKey:@"magnitude"];
|
||||
switch ([intensity intValue]) {
|
||||
case 0:
|
||||
@@ -111,11 +77,8 @@
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
else if ([[userInfo objectForKey:@"type"] isEqualToString:@"official"]) {
|
||||
|
||||
} else if ([[userInfo objectForKey:@"type"] isEqualToString:@"official"]) {
|
||||
NSString *provaider = [userInfo objectForKey:@"provider"];
|
||||
double intensity = [[userInfo objectForKey:@"magnitude"] doubleValue];
|
||||
|
||||
@@ -129,7 +92,7 @@
|
||||
colore = @"_red";
|
||||
else
|
||||
colore = @"_purple";
|
||||
|
||||
|
||||
if ([provaider isEqualToString:@"USGS"])
|
||||
stringURL = [stringURL stringByAppendingString:[NSString stringWithFormat:@"star%@2.png", colore]];
|
||||
if ([provaider isEqualToString:@"SGC"])
|
||||
@@ -164,9 +127,7 @@
|
||||
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"]){
|
||||
|
||||
} else if ([[userInfo objectForKey:@"type"] isEqualToString:@"tsunami"]){
|
||||
NSString *color = [userInfo objectForKey:@"color"];
|
||||
|
||||
if ([color isEqualToString:@"gr"])
|
||||
@@ -181,23 +142,20 @@
|
||||
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];
|
||||
|
||||
}];
|
||||
|
||||
self.bestAttemptContent.attachments = [NSArray arrayWithObjects:attachment, nil];
|
||||
|
||||
if (![messaggioModificato isEqualToString:@"non modificato"])
|
||||
self.bestAttemptContent.body = [NSString stringWithFormat:@"%@", messaggioModificato];
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
-(NSString *)setMessage:(NSString *)message{
|
||||
|
||||
- (NSString *)setMessage:(NSString *)message
|
||||
{
|
||||
BOOL warning = false;
|
||||
BOOL watch = false;
|
||||
BOOL threat = false;
|
||||
@@ -223,46 +181,40 @@
|
||||
if ([message.lowercaseString containsString:@"supplement"])
|
||||
supplement = YES;
|
||||
|
||||
|
||||
if (warning){
|
||||
if (warning) {
|
||||
if (supplement)
|
||||
_message = NSLocalizedString(@"Supplemento di allerta", @"");
|
||||
_message = NSLocalizedString(@"Supplemento di allerta", @"");
|
||||
else if(cancellation)
|
||||
_message = NSLocalizedString(@"Allerta cancellata", @"");
|
||||
_message = NSLocalizedString(@"Allerta cancellata", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Allerta Tsunami", @"");
|
||||
}
|
||||
else if (watch){
|
||||
_message = NSLocalizedString(@"Allerta Tsunami", @"");
|
||||
} else if (watch) {
|
||||
if (supplement)
|
||||
_message = NSLocalizedString(@"Supplemento di valutazione", @"");
|
||||
_message = NSLocalizedString(@"Supplemento di valutazione", @"");
|
||||
else if(cancellation)
|
||||
_message = NSLocalizedString(@"Valutazione cancellata", @"");
|
||||
_message = NSLocalizedString(@"Valutazione cancellata", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Valutazione Tsunami", @"");
|
||||
}
|
||||
|
||||
else if (threat){
|
||||
_message = NSLocalizedString(@"Valutazione Tsunami", @"");
|
||||
} else if (threat) {
|
||||
if (threatFinal)
|
||||
_message = NSLocalizedString(@"Pericolo cessato", @"");
|
||||
_message = NSLocalizedString(@"Pericolo cessato", @"");
|
||||
else
|
||||
_message = NSLocalizedString(@"Pericolo tsunami", @"");
|
||||
}
|
||||
else if (information){
|
||||
_message = NSLocalizedString(@"Pericolo tsunami", @"");
|
||||
} else if (information){
|
||||
_message = NSLocalizedString(@"Messaggio informativo", @"");
|
||||
}
|
||||
else if (supplement){
|
||||
} else if (supplement){
|
||||
_message = NSLocalizedString(@"Messaggio supplementare", @"");
|
||||
}
|
||||
return _message;
|
||||
|
||||
}
|
||||
|
||||
- (void)serviceExtensionTimeWillExpire {
|
||||
- (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);
|
||||
}
|
||||
|
||||
@@ -277,37 +229,37 @@
|
||||
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);
|
||||
}];
|
||||
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];
|
||||
}
|
||||
@@ -317,4 +269,5 @@
|
||||
[self.session invalidateAndCancel];
|
||||
self.contentHandler(self.bestAttemptContent);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user