iosapple-push-notificationsunnotificationserviceextension

Will iOS Notification Service Extension delete attached file from device?


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

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.

See the code bellow:

1.Schedule a local notification

+ (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 (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
}

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.

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.

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.


Solution

  • Just found in documentation

    UNNotificationAttachment:

    The system validates the content of attached files before scheduling the corresponding notification request. If an attached file is corrupted, invalid, or of an unsupported file type, the notification request is not scheduled for delivery. Once validated, attached files are moved into the attachment data store so that they can be accessed by the appropriate processes. Attachments located inside an app’s bundle are copied instead of moved.