0

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;
}
Crazy-Fool
  • 96
  • 1
  • 13

1 Answers1

0

Check if you set up the key NSUserNotificationAlertStyle

Check the system response((BOOL granted, NSError * _Nullable error)) if you get an error, please provide it.

Add notification in the completion block.

Do not run your code in main, app might haven't been properly initialized, use AppDelegate appDidFinishLaunching instead.

Test with triggerWithTimeInterval:[NSDate date].timeIntervalSince1970+30 (it contradicts documentation but works in my case)

Collapse you app in delivery time, it might be not delivered when app is in focus.

Also go to "System Preferences" and check if your app is allowed to post notifications.(If you do not manage to find your app in the list, please, let us know, it might be the key to the problem)

Nuzhdin Vladimir
  • 1,714
  • 18
  • 36