iOS 通知服务扩展会从设备中删除附加文件吗?

rob*_*oby 5 apple-push-notifications ios unnotificationserviceextension

我遇到了一个奇怪的问题。iOS 通知服务扩展将从设备中删除附件。

我使用 SDWebImage 来显示和缓存图像,并实现了通知服务扩展以在通知警报视图中显示图像。

就我而言,图像已在本地缓存。然后,我单击主页按钮,我的应用程序在后台运行,应用程序安排了一个本地通知,并将缓存的图像附加到通知内容中。

请参阅下面的代码:

1.安排本地通知

+ (void)postLocalNotificationGreaterThanOrEqualToiOS10:(LNotification)module body:(NSDictionary *)body {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.sound = [UNNotificationSound defaultSound];
content.body = @"body";
content.userInfo = @{};

//get the image in device to attach into notification
NSError *error;
NSString* imgURL = [body valueForKey:kLocalNotification_Image];
NSString *filePath = [[SDImageCache sharedImageCache] defaultCachePathForKey:imgURL];
NSURL *url = [NSURL URLWithString:[@"file://" stringByAppendingString:filePath]];
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"Image" URL:url options:nil error:&error];
if (attachment) {
    content.attachments = @[attachment];
}

UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];

UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond"
                                                                      content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error) {
        NSLog(@"error: %@", error.localizedDescription);
    }
}];
} 
Run Code Online (Sandbox Code Playgroud)

2.通知服务扩展(实际上,对于本地通知,只调用了didReceiveNotificationRequest:withContentHandler:,并没有做任何事情。)

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
self.contentHandler = contentHandler;
self.bestAttemptContent = [request.content mutableCopy];

NSDictionary *aps = [self.bestAttemptContent.userInfo objectForKey:@"aps"];
if (aps) {
    ....//For remote notification, modify the notification content here
}
else {
    //For local notification, do nothing
}

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);
}

- (NSString *)downloadImageWithURL:(NSString *)imageURLString imageName:(NSString *)imageName {
    ....//code will not execute for local notification
}
Run Code Online (Sandbox Code Playgroud)

我发现,我附加到本地通知中的图像将从设备中删除。我的意思是,当我单击通知警报从后台启动应用程序到前台后,我尝试显示图像,不幸的是,SDWebImageCache 没有从磁盘或内存中找到缓存。

我看了iOS API的偏好,没有发现附件会被删除。有谁知道我在哪里可以找到这个问题的线索?也许我只是忽略了一些重要的内容,请参阅通知服务扩展。

现在,我做了一个临时解决方案来解决这个问题,在安排本地通知时,复制缓存的图像并另存为另一个名称,然后将其附加到本地通知中。即使通知服务扩展也会删除附件,它只会删除副本文件,应用程序将从缓存中找到图像。

但是,我真的很想知道为什么会出现这个问题。提前致谢。

DoN*_*1cK 5

刚刚在文档中找到

\n\n

UN通知附件:

\n\n
\n

系统在安排相应的通知请求之前验证附加文件的内容。如果附加文件已损坏、无效或文件类型不受支持,则不会安排传送通知请求。验证后,附加文件将移至附件数据存储中,以便适当的进程可以访问它们。位于 app\xe2\x80\x99s\n 捆绑包内的附件将被复制而不是移动。

\n
\n