用户通知 使用 UNUser通知中心,在系统偏好设置中设置警报



我正在使用MacOS Mojave,我从使用NSUserNotification(现已弃用(切换到UNUserNotificationCenter。我的 App 显示在"系统偏好设置通知"中,并选中了横幅样式。横幅样式是否始终为默认值?我真的很想从警报样式开始,以便用户可以看到可用的按钮。

    // 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");
        }
    }];

联系了苹果开发者支持,我没有做错任何事。Apple将通知设计为横幅,即使我使用UNAuthorizationOptions请求警报。所以现在我知道了。

最新更新