diff options
author | Vitaly Takmazov | 2018-10-18 23:09:39 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-10-18 23:09:39 +0300 |
commit | ebd483ee5880131d6602e63ca46cfa49caef2983 (patch) | |
tree | 71bf2959f44189f2bee743b9fb803ebb210d88d7 /JuickPush | |
parent | 0b32db6c12b5a01c8d1b6e2e8f0d3089c4aa2ae8 (diff) |
Avatars in notifications
Diffstat (limited to 'JuickPush')
-rw-r--r-- | JuickPush/Info.plist | 31 | ||||
-rw-r--r-- | JuickPush/NotificationService.h | 13 | ||||
-rw-r--r-- | JuickPush/NotificationService.m | 46 |
3 files changed, 90 insertions, 0 deletions
diff --git a/JuickPush/Info.plist b/JuickPush/Info.plist new file mode 100644 index 0000000..935f4fa --- /dev/null +++ b/JuickPush/Info.plist @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>CFBundleDevelopmentRegion</key> + <string>$(DEVELOPMENT_LANGUAGE)</string> + <key>CFBundleDisplayName</key> + <string>JuickPush</string> + <key>CFBundleExecutable</key> + <string>$(EXECUTABLE_NAME)</string> + <key>CFBundleIdentifier</key> + <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> + <key>CFBundleInfoDictionaryVersion</key> + <string>6.0</string> + <key>CFBundleName</key> + <string>$(PRODUCT_NAME)</string> + <key>CFBundlePackageType</key> + <string>XPC!</string> + <key>CFBundleShortVersionString</key> + <string>1.0</string> + <key>CFBundleVersion</key> + <string>1</string> + <key>NSExtension</key> + <dict> + <key>NSExtensionPointIdentifier</key> + <string>com.apple.usernotifications.service</string> + <key>NSExtensionPrincipalClass</key> + <string>NotificationService</string> + </dict> +</dict> +</plist> diff --git a/JuickPush/NotificationService.h b/JuickPush/NotificationService.h new file mode 100644 index 0000000..91ca27a --- /dev/null +++ b/JuickPush/NotificationService.h @@ -0,0 +1,13 @@ +// +// NotificationService.h +// JuickPush +// +// Created by Vitaly Takmazov on 18/10/2018. +// Copyright © 2018 com.juick. All rights reserved. +// + +#import <UserNotifications/UserNotifications.h> + +@interface NotificationService : UNNotificationServiceExtension + +@end diff --git a/JuickPush/NotificationService.m b/JuickPush/NotificationService.m new file mode 100644 index 0000000..82dead8 --- /dev/null +++ b/JuickPush/NotificationService.m @@ -0,0 +1,46 @@ +// +// NotificationService.m +// JuickPush +// +// Created by Vitaly Takmazov on 18/10/2018. +// Copyright © 2018 com.juick. 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]; + + NSURL *avatarURL = [NSURL URLWithString:request.content.userInfo[@"avatarUrl"]]; + + NSURLSession * downloadSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; + + [[downloadSession downloadTaskWithURL:avatarURL completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { + if (!error) { + NSError *err = nil; + NSString *temporaryDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:[response suggestedFilename]]; + [[NSFileManager defaultManager] moveItemAtPath:location.path toPath:temporaryDirectory error:&err]; + UNNotificationAttachment *avatarAttachment = [UNNotificationAttachment attachmentWithIdentifier:avatarURL.lastPathComponent URL:[NSURL fileURLWithPath:temporaryDirectory] options:nil error:&err]; + self.bestAttemptContent.attachments = @[avatarAttachment]; + self.contentHandler(self.bestAttemptContent); + } + }] resume]; +} + +- (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); +} + +@end |