- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 UNNotificationServiceExtension
在 iOS 10 上创建带有图像附件的丰富通知。一切都适用于较小的图像,除非要下载的图像稍大( e.g. 7MB )。
在这种情况下,下载开始但从未完成,下载开始后几乎立即显示“最佳尝试”通知。
根据 Apple's docs ,图像最大可达 10MB,因此大小无关紧要:)
我确实实现了 serviceExtensionTimeWillExpire
,iOS 应该通知我必须尽快交付内容,但是这个方法没有被调用所以我想知道是什么正在发生。
更新 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
程序以退出代码结束:0
是可疑的。知道发生了什么事吗?
谢谢简
最佳答案
所以解决方案是使用 NSURLSessionDownloadTask
而不是 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];
关于objective-c - 在 UNNotificationServiceExtension 中下载大图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43118492/
我有两种尺寸的图像(垂直和水平),它们将始终具有相同的尺寸,恭敬。我正在尝试创建一个容器来容纳图像但不会将内容推到上面并保持相似的高度或宽度。我也不想显示完整尺寸的图像,所以我在考虑使用 overfl
我有一个使用该类的 LaTeX 文档 \documentclass[12pt,a4paper]{scrbook} 我更改了一些用于定位浮点数的参数: \renewcommand{\topfractio
我有一个问题想和你分享。 所以就在这里。想象一下,我有一个非常大的图像,当我打开我的页面时它需要很重的负载。如果我想要将图像剪切成许多小块并在加载图像时将它们一个接一个地合并(只是 javascrip
我正在学习 Java SE 6.0。 是否有一张大图或图表来说明 Java 6.0 的所有类以及它们之间的关系? 提前致谢。 尊敬的 stackoverflow 开发人员和程序员。 你好。非常感谢您对
我得到了一个 InflateException 并且抛出了一个 OutOfMemoryError 异常。我知道我应该能够通过减小图像的大小/分辨率,或者通过将 android:largeHeap="t
我必须引用 id 从服务器到 android 移动设备检索所有大小的图像,所以我尝试使用下面的编码并成功获得 base 64 字符串并检索等效 base64string 的图像它适用于小尺寸图像但当我
我是一名优秀的程序员,十分优秀!