Uploading attached files
Note.
You can add attachments in the Push API using the Sending push messages operation (the attachments
parameter).
Configure uploading attached files in push notifications by calling the downloadAttachmentsForNotificationRequest method (objective-c/swift):
- Objective-C
-
@implementation NotificationService - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; [YMPYandexMetricaPush setExtensionAppGroup:@"EXTENSION_AND_APP_SHARED_APP_GROUP_NAME"]; [YMPYandexMetricaPush handleDidReceiveNotificationRequest:request]; [YMPYandexMetricaPush downloadAttachmentsForNotificationRequest:request callback:^(NSArray<UNNotificationAttachment *> *attachments, NSError *error) { if (error != nil) { NSLog(@"Error: %@", error); } [self completeWithBestAttemptAndAttachments:attachments]; }]; } - (void)serviceExtensionTimeWillExpire { [self completeWithBestAttemptAndAttachments:nil]; } - (void)completeWithBestAttemptAndAttachments:(NSArray<UNNotificationAttachment *> *)attachments { @synchronized (self) { if (self.contentHandler != nil) { if (attachments != nil) { self.bestAttemptContent.attachments = attachments; } self.contentHandler(self.bestAttemptContent); self.contentHandler = nil; } } } @end
Copied to clipboard - Swift
-
class NotificationService: UNNotificationServiceExtension { private var contentHandler: ((UNNotificationContent) -> Void)? private var bestAttemptContent: UNMutableNotificationContent? private let syncQueue = DispatchQueue(label: "NotificationService.syncQueue") override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { self.contentHandler = contentHandler self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent) // ... YMPYandexMetricaPush.downloadAttachments(for: request) { attachments, error in if let error = error { print("Error: \(error)") } self.completeWithBestAttempt(attachments: attachments) } } override func serviceExtensionTimeWillExpire() { completeWithBestAttempt(attachments: nil) } func completeWithBestAttempt(attachments: [UNNotificationAttachment]?) { syncQueue.sync { if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent { if let attachments = attachments { bestAttemptContent.attachments = attachments } contentHandler(bestAttemptContent) self.bestAttemptContent = nil self.contentHandler = nil } } } }
Copied to clipboard