4

我正在使用UNNotificationServiceExtension在 iOS 10 上创建带有图像附件的丰富通知。除非要下载的图像稍大一些(例如 7MB),否则一切都适用于较小的图像。

在这种情况下,下载开始但从未完成,并且“最佳尝试”通知几乎在下载开始后立即显示。

根据Apple 的文档,图像最大可达 10MB,因此大小无关紧要:)

我确实实现了serviceExtensionTimeWillExpireiOS 应该通知我必须尽快交付内容的地方,但没有调用此方法,所以我想知道发生了什么。

更新 1 我还注意到相同的图像在本地存储时可以正常工作。所以看起来问题在于下载图像。但是,如前所述,下载几乎立即被终止。

实施很简单

- (BOOL)handleNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *_Nonnull))contentHandler
{

    self.bestAttemptContent = (UNMutableNotificationContent *) [request.content mutableCopy];

    UNMutableNotificationContent *content = [self.bestAttemptContent mutableCopy];

    NSString *urlString = [content.userInfo valueForKeyPath:@"attachment-url"];

    NSLog(@"Downloading notification attachment completed with: %@", url.absoluteString);
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *_Nullable data, NSURLResponse *_Nullable response, NSError *_Nullable error) {

        NSLog(@"Downloading notification attachment %@", error == nil ? @"success" : [NSString stringWithFormat:@"error: %@", error]);

        NSError *fileError;
        UNNotificationAttachment *attachment = [UNNotificationAttachment d360_createWithFileName:url.lastPathComponent identifier:url.absoluteString data:data options:nil error:&fileError];

        if (!attachment) {
            NSLog(@"Could not create local attachment file: %@", fileError);
            contentHandler(content);
            return;
        }

        NSLog(@"Adding attachment: %@", attachment);

        NSMutableArray *attachments = content.attachments ? [content.attachments mutableCopy] : [NSMutableArray array];

        [attachments addObject:attachment];

        content.attachments = attachments;
        contentHandler(content);


    }];
    [task resume];


    return YES;
}

- (void)serviceExtensionTimeWillExpire
{
    NSLog(@"Service extension expired. Using best attempt content  %@", self.bestAttemptContent);
    self.contentHandler(self.bestAttemptContent);
}

当我在 XCode 中调试它时,我看到以下日志:

2017-03-30 14:51:43.723669 D360TestAppNotificationExtension[3393:398992] [D360Extension]: Handling notification request content
2017-03-30 14:51:43.724103 D360TestAppNotificationExtension[3393:398992] [D360Extension]: Downloading notification attachment: https://upload.wikimedia.org/wikipedia/commons/b/b2/Bled_Castle_05.jpg
Program ended with exit code: 0

Program ended with exit code: 0是可疑的。知道发生了什么吗?

谢谢简

4

1 回答 1

4

所以解决方案是使用 aNSURLSessionDownloadTask而不是 a NSURLSessionDataTask。在这种情况下,下载内容被保存到一个临时位置并使用更少的内存。然后UNNotificationAttachment可以直接从临时文件创建。

请注意,UNNotificationAttachment需要读取具有受支持文件扩展名的文件,因此我只需将远程文件名附加到本地临时文件 URL

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:url completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {

    NSLog(@"Downloading notification attachment completed with %@", error == nil ? @"success" : [NSString stringWithFormat:@"error: %@", error]);

    NSError *fileError;
    // create a local URL with extension
    NSURL *urlWithExtension = [NSURL fileURLWithPath:[location.path stringByAppendingString:url.lastPathComponent]];

    if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:urlWithExtension error:&fileError]) {
        NSLog(@"Could not append  local attachment file name: %@", fileError);
        contentHandler(content);
        return;
    }

    UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:url.absoluteString
                                                                                          URL:urlWithExtension options:nil
                                                                                        error:&fileError];

    if (!attachment) {
        NSLog(@"Could not create local attachment file: %@", fileError);
        contentHandler(content);
        return;
    }

    NSLog(@"Adding attachment: %@", attachment);

    NSMutableArray *attachments = content.attachments ? [content.attachments mutableCopy] : [NSMutableArray array];

    [attachments addObject:attachment];

    content.attachments = attachments;

    contentHandler(content);


}];
[task resume];
于 2017-03-31T12:07:55.003 回答