gpt4 book ai didi

macos - 使用 UNUserNotificationCenter 的用户通知以及系统偏好设置中的警报设置

转载 作者:行者123 更新时间:2023-12-03 17:09:01 51 4
gpt4 key购买 nike

我使用的是 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");
}
}];

最佳答案

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

关于macos - 使用 UNUserNotificationCenter 的用户通知以及系统偏好设置中的警报设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55463930/

51 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com