This is the (simplified) code I am trying to run. The image file exists and the app does not crash. What happens in that the image is deleted from the location as mentioned in the documentation for UserNotifications. However, the image thumbnail does not show up in the notification that is generated.
I am not sure what else I am missing here.
#import <UserNotifications/UserNotifications.h>
int main(int argc, const char * argv[]) {
UNUserNotificationCenter* notificationCenter = [UNUserNotificationCenter currentNotificationCenter];
[notificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {}
];
UNMutableNotificationContent *localNotification = [UNMutableNotificationContent new];
localNotification.title = [NSString localizedUserNotificationStringForKey:@"Title" arguments:nil];
localNotification.body = [NSString localizedUserNotificationStringForKey:@"Body Text" arguments:nil];
localNotification.sound = [UNNotificationSound defaultSound];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)lastObject];
NSString *testUrl = [path stringByAppendingPathComponent:@"imageFile.jpg"];
NSURL* url = [NSURL fileURLWithPath:testUrl];
CGRect rect = CGRectMake(0.25, 0.25, 0.75, 0.75);
NSDictionary* options = @ {
@"UNNotificationAttachmentOptionsTypeHintKey": (__bridge NSString*) kUTTypeJPEG,
@"UNNotificationOptionsThumbnailHiddenKey" : @NO,
@"UNNotificationAttachmentOptionsThumbnailClippingRectKey": [NSValue valueWithRect: rect]
};
UNNotificationAttachment* imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@""
URL:url
options:options
error:nil];
localNotification.attachments=@[imageAttachment];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"Identifier" content: localNotification trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"Notification created");
}];
[NSThread sleepForTimeInterval:100.0f];
return 0;
}