Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 用户通知使用UNUserNotificationCenter,并在系统首选项中设置警报_Macos_Cocoa - Fatal编程技术网

Macos 用户通知使用UNUserNotificationCenter,并在系统首选项中设置警报

Macos 用户通知使用UNUserNotificationCenter,并在系统首选项中设置警报,macos,cocoa,Macos,Cocoa,我使用的是MacOS Mojave,我从使用NSUserNotification(现已弃用)切换到了UNUserNotificationCenter。“我的应用”将显示在系统首选项通知中,并选择横幅样式。横幅样式始终是默认样式吗?我真的想从警报样式开始,这样用户就可以看到可用的按钮 // 03-27-2019 commented. has absolutely no effect on the notification appearing UNAuthorizationOptio

我使用的是MacOS Mojave,我从使用NSUserNotification(现已弃用)切换到了UNUserNotificationCenter。“我的应用”将显示在系统首选项通知中,并选择横幅样式。横幅样式始终是默认样式吗?我真的想从警报样式开始,这样用户就可以看到可用的按钮

    // 03-27-2019 commented. has absolutely no effect on the notification appearing
    UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge | UNAuthorizationOptionProvidesAppNotificationSettings;
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:options


联系了苹果开发者支持,我没有做错任何事。苹果将这些通知设计成横幅,即使我请求带有未经授权选项的警报。现在我知道了

UNNotificationAction* snoozeAction = [UNNotificationAction
                                      actionWithIdentifier:@"SNOOZE_ACTION"
                                      title:@"Snooze"
                                      options:UNNotificationActionOptionNone];  // 03-25-2019 The action has the default behavior.

UNNotificationAction* stopAction = [UNNotificationAction
                                    actionWithIdentifier:@"STOP_ACTION"
                                    title:@"Stop"
                                    options:UNNotificationActionOptionForeground];  // 03-25-2019 The action causes the app to launch in the foreground.

// start 03-27-2019
UNNotificationAction* bogusAction = [UNNotificationAction
                                     actionWithIdentifier:@"BOGUS_ACTION"
                                     title:@"Bogus"
                                     options:UNNotificationActionOptionForeground];  // 03-25-2019 The action causes the app to launch in the foreground.

                                                                    completionHandler:^(BOOL granted, NSError * _Nullable error) {
                                                                        if (!granted) {
                                                                            NSLog(@"requestAuthorizationWithOptions: NO");
                                                                        } else {
                                                                            NSLog(@"requestAuthorizationWithOptions: YES");
                                                                       }
UNNotificationCategory* expiredCategoryPlus = [UNNotificationCategory
                                               categoryWithIdentifier:@"TIMER_EXPIRED_PLUS"
                                                   actions:@[snoozeAction, stopAction, bogusAction]   // 03-27-2019 show just 1 button with "Actions": snooze, stop, bogus

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

[center setNotificationCategories:[NSSet setWithObjects:expiredCategoryPlus, // 03-28-2019

// display the notification
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString localizedUserNotificationStringForKey:@"Wake up!" arguments:nil];
content.subtitle = [NSString localizedUserNotificationStringForKey:@"The subtitle." arguments:nil];
content.body = [NSString localizedUserNotificationStringForKey:@"Rise and shine! It's morning time!"
                                                         arguments:nil];
content.sound = [UNNotificationSound defaultSound];
content.attachments = @[]; 
content.categoryIdentifier = @"TIMER_EXPIRED_PLUS";

    // configure trigger for right now
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitTimeZone fromDate:now];

    // set the trigger
    UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
                                              //                                              triggerWithDateMatchingComponents:date repeats:NO];
                                              triggerWithDateMatchingComponents:components repeats:NO];

    // Create the request object.
    UNNotificationRequest* request = [UNNotificationRequest
                                      requestWithIdentifier:@"MorningAlarm" content:content trigger:trigger];

    // Schedule the notification.
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    //        [center addNotificationRequest:request];  // 02-23-2019 don't compile
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        if (!error) {
            NSLog(@"Local Notification succeeded");
        }
        else {
            NSLog(@"Local Notification failed");
        }
    }];