【问题标题】:Will iOS Notification Service Extension delete attached file from device?iOS 通知服务扩展会从设备中删除附件吗?
【发布时间】:2018-02-10 11:54:49
【问题描述】:

我遇到了一个奇怪的问题。 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);
    }
}];
} 

2.Notification Service Extension(其实对于本地通知,只调用了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
}

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

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

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

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

【问题讨论】:

  • 是的,我也经历过同样的行为。如果我找到它们,我会发布官方文档。
  • @NikhilManapure 谢谢!我试过了,但没有找到官方文档提到这个问题。但是,我已经解决了这个问题,并且效果很好。至少,不仅我遇到了这个问题。希望我们能弄清楚。
  • 看来我也遇到过这个问题。你是怎么解决的?制作了附件文件的副本?
  • @DoN1cK 是的,我只是在安排本地通知之前复制了一个附件文件,并将复制的文件传递给本地通知以防止缓存文件被删除。

标签: ios apple-push-notifications unnotificationserviceextension


【解决方案1】:

刚刚在文档中找到

UNNotificationAttachment:

系统在调度前验证附件的内容 相应的通知请求。如果附件是 已损坏、无效或文件类型不受支持的通知 请求未安排交付。验证后,附加文件 被移动到附件数据存储中,以便可以访问它们 通过适当的过程。位于应用程序内部的附件 捆绑包被复制而不是移动。

【讨论】:

  • 谢谢!你开枪了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2020-03-13
  • 1970-01-01
  • 2021-06-13
相关资源
最近更新 更多