Reputation: 159
I'm using MacOS Mojave and I switched from using NSUserNotification, which is now deprecated, to UNUserNotificationCenter. My app appears in System Preferences Notifications with a banner style selected. Is the banner style always the default? I really want to start with an alerts style so the user can see the buttons that are available.
// 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");
}
}];
Upvotes: 1
Views: 1009
Reputation: 159
Contacted Apple Developer Support and I'm not doing anything wrong. Apple designed the notifications to be banners, even though I request alerts with UNAuthorizationOptions. So now I know.
Upvotes: 2