本文介绍了iOS Notification Service Extension会从设备中删除附件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题.iOS Notification Service Extension将从设备中删除附件.

I got a strange problem. iOS Notification Service Extension will delete the attachment from device.

我使用SDWebImage来显示和缓存图像,并且实现了Notification Service Extension以在通知警报视图中显示图像.

I use SDWebImage to display and cache image, and I implemented a Notification Service Extension to display a image in the notification alert view.

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

In my case, the image was already cached locally. Then, I click home button, my app was running in background, app scheduled a local notification with the cached image attach into the notification content.

请参见下面的代码:

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.通知服务扩展(实际上,对于本地通知,只调用didReceiveNotificationRequest:withContentHandler:而没有执行任何操作.)

2.Notification Service Extension (In Fact, for local notification, only didReceiveNotificationRequest:withContentHandler: was called and did nothing.)

- (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既没有从磁盘也没有从内存中找到缓存.

I found that, the image I attached into the local notification will be deleted from device. I mean, after I click the notification alert to launch app from background to foreground, I try to display the image, unfortunately, SDWebImageCache did not find the cache from neither disk nor memory.

我阅读了iOS API的首选项,但没有发现附件将被删除.有人知道我在哪里可以找到这个问题的任何线索?也许我只是忽略了一些有关Notification Service Extension的重要内容.

I read the preference of iOS API, and didn't find that the attachment will be deleted. Does anybody know where could I find any clue of this issue? Maybe I just ignored something important refer to Notification Service Extension.

现在,我已临时解决此问题,同时计划了本地通知,同时复制缓存的图像并另存为另一个名称,然后将其附加到本地通知中.甚至Notification Service Extension也会删除附件,它只会删除副本文件,而应用程序会从缓存中找到图像.

Now, I made a work around to fix this issue temporarily, while scheduling local notification, copy the cached image and save as another name, then attach it into local notification. Even Notification Service Extension will delete the attachment, it will just deleted the copy file, and the app will find the image from cache.

但是,我真的想知道为什么发生此问题.预先感谢.

But, I really wanna know why this problem happened. Thanks in advance.

推荐答案

仅在文档中找到

UNNotificationAttachment:

这篇关于iOS Notification Service Extension会从设备中删除附件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:39