mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-08-05 16:58:59 +00:00
74 lines
3.1 KiB
Mathematica
74 lines
3.1 KiB
Mathematica
![]() |
//
|
||
|
// NotificationService.m
|
||
|
// Story Notification Service Extension
|
||
|
//
|
||
|
// Created by Samuel Clay on 11/21/16.
|
||
|
// Copyright © 2016 NewsBlur. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#import "NotificationService.h"
|
||
|
|
||
|
@interface NotificationService ()
|
||
|
|
||
|
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
|
||
|
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
|
||
|
|
||
|
@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];
|
||
|
NSString *imageUrl = [request.content.userInfo objectForKey:@"image_url"];
|
||
|
NSLog(@"Attaching image: %@", imageUrl);
|
||
|
if (imageUrl) {
|
||
|
[self loadAttachmentForUrlString:imageUrl completionHandler:^(UNNotificationAttachment *attachment) {
|
||
|
if (attachment) {
|
||
|
self.bestAttemptContent.attachments = @[attachment];
|
||
|
}
|
||
|
self.contentHandler(self.bestAttemptContent);
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
self.contentHandler(self.bestAttemptContent);
|
||
|
}
|
||
|
|
||
|
- (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.contentHandler(self.bestAttemptContent);
|
||
|
}
|
||
|
|
||
|
- (void)loadAttachmentForUrlString:(NSString *)urlString
|
||
|
completionHandler:(void(^)(UNNotificationAttachment *))completionHandler {
|
||
|
|
||
|
__block UNNotificationAttachment *attachment = nil;
|
||
|
NSURL *attachmentURL = [NSURL URLWithString:urlString];
|
||
|
|
||
|
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
|
||
|
[[session downloadTaskWithURL:attachmentURL
|
||
|
completionHandler:^(NSURL *temporaryFileLocation, NSURLResponse *response, NSError *error) {
|
||
|
if (error != nil) {
|
||
|
NSLog(@"%@", error.localizedDescription);
|
||
|
} else {
|
||
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||
|
NSURL *localURL = [NSURL fileURLWithPath:temporaryFileLocation.path];
|
||
|
[fileManager moveItemAtURL:temporaryFileLocation toURL:localURL error:&error];
|
||
|
|
||
|
NSError *attachmentError = nil;
|
||
|
attachment = [UNNotificationAttachment attachmentWithIdentifier:@"" URL:localURL options:nil error:&attachmentError];
|
||
|
if (attachmentError) {
|
||
|
NSLog(@"%@", attachmentError.localizedDescription);
|
||
|
}
|
||
|
}
|
||
|
completionHandler(attachment);
|
||
|
}] resume];
|
||
|
}
|
||
|
|
||
|
|
||
|
@end
|