MacOS Mojave UNNotificationAttachment缩略图未显示在通知中

时间:2019-01-26 22:03:06

标签: macos macos-mojave usernotifications

这是我尝试运行的(简化)代码。图像文件存在,应用程序不会崩溃。如从UserNotifications文档中所述,将图像从该位置删除会发生什么情况。但是,图像缩略图不会显示在生成的通知中。

我不确定这里还缺少什么。

#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;
}

1 个答案:

答案 0 :(得分:0)

检查是否设置了密钥NSUserNotificationAlertStyle

如果发现错误,请检查系统响应((已授予BOOL,NSError * _Nullable错误))。

在完成块中添加通知。

请勿在main中运行您的代码,应用可能未正确初始化,请改用AppDelegate appDidFinishLaunching。

使用triggerWithTimeInterval进行测试:[NSDate date] .timeIntervalSince1970 + 30(与文档相矛盾,但对我而言有效)

在交付时间中使您的应用程序崩溃,当您关注应用程序时,它可能不会交付。

也请转到“系统偏好设置”,检查是否允许您的应用发布通知。(如果您无法在列表中找到您的应用,请告诉我们,这可能是问题的关键)