Last active
July 14, 2022 08:55
-
-
Save jzucker2/31ace28514940fae047ec89a8317ba7c to your computer and use it in GitHub Desktop.
UserNotifications on iOS 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to include this at the top of AppDelegate.m | |
@import UserNotifications; | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
// Override point for customization after application launch. | |
// Check Notification Settings on launch | |
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { | |
switch (settings.authorizationStatus) { | |
// This means we have not yet asked for notification permissions | |
case UNAuthorizationStatusNotDetermined: | |
{ | |
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { | |
// You might want to remove this, or handle errors differently in production | |
NSAssert(error == nil, @"There should be no error"); | |
if (granted) { | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
}]; | |
} | |
break; | |
// We are already authorized, so no need to ask | |
case UNAuthorizationStatusAuthorized: | |
{ | |
// Just try and register for remote notifications | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
break; | |
// We are denied User Notifications | |
case UNAuthorizationStatusDenied: | |
{ | |
// Possibly display something to the user | |
UIAlertController *useNotificationsController = [UIAlertController alertControllerWithTitle:@"Turn on notifications" message:@"This app needs notifications turned on for the best user experience" preferredStyle:UIAlertControllerStyleAlert]; | |
UIAlertAction *goToSettingsAction = [UIAlertAction actionWithTitle:@"Go to settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { | |
}]; | |
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]; | |
[useNotificationsController addAction:goToSettingsAction]; | |
[useNotificationsController addAction:cancelAction]; | |
[self.window.rootViewController presentViewController:useNotificationsController animated:true completion:nil]; | |
NSLog(@"We cannot use notifications because the user has denied permissions"); | |
} | |
break; | |
} | |
}]; | |
return YES; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to include this at the top of AppDelegate.swift | |
import UserNotifications | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
// Check Notification Settings on launch | |
UNUserNotificationCenter.current().getNotificationSettings { (settings) in | |
switch settings.authorizationStatus { | |
// This means we have not yet asked for notification permissions | |
case .notDetermined: | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (granted, error) in | |
// You might want to remove this, or handle errors differently in production | |
assert(error == nil) | |
if granted { | |
UIApplication.shared.registerForRemoteNotifications() | |
} | |
}) | |
// We are already authorized, so no need to ask | |
case .authorized: | |
// Just try and register for remote notifications | |
UIApplication.shared.registerForRemoteNotifications() | |
case .denied: | |
// Possibly display something to the user | |
let useNotificationsAlertController = UIAlertController(title: "Turn on notifications", message: "This app needs notifications turned on for the best user experience", preferredStyle: .alert) | |
let goToSettingsAction = UIAlertAction(title: "Go to settings", style: .default, handler: { (action) in | |
}) | |
let cancelAction = UIAlertAction(title: "Cancel", style: .default) | |
useNotificationsAlertController.addAction(goToSettingsAction) | |
useNotificationsAlertController.addAction(cancelAction) | |
self.window?.rootViewController?.present(useNotificationsAlertController, animated: true) | |
print("We cannot use notifications because the user has denied permissions") | |
} | |
} | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to include this at the top of AppDelegate.m | |
@import UserNotifications; | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | |
// Override point for customization after application launch. | |
// We want to check Notification Settings on launch. | |
// First we must determine your iOS type: | |
// Note this will only work for iOS 8 and up, if you require iOS 7 notifications then | |
// contact [email protected] with your request | |
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue]; | |
if (systemVersion >= 10) { | |
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { | |
switch (settings.authorizationStatus) { | |
// This means we have not yet asked for notification permissions | |
case UNAuthorizationStatusNotDetermined: | |
{ | |
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) { | |
// You might want to remove this, or handle errors differently in production | |
NSAssert(error == nil, @"There should be no error"); | |
if (granted) { | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
}]; | |
} | |
break; | |
// We are already authorized, so no need to ask | |
case UNAuthorizationStatusAuthorized: | |
{ | |
// Just try and register for remote notifications | |
[[UIApplication sharedApplication] registerForRemoteNotifications]; | |
} | |
break; | |
// We are denied User Notifications | |
case UNAuthorizationStatusDenied: | |
{ | |
// Possibly display something to the user | |
UIAlertController *useNotificationsController = [UIAlertController alertControllerWithTitle:@"Turn on notifications" message:@"This app needs notifications turned on for the best user experience" preferredStyle:UIAlertControllerStyleAlert]; | |
UIAlertAction *goToSettingsAction = [UIAlertAction actionWithTitle:@"Go to settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { | |
}]; | |
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil]; | |
[useNotificationsController addAction:goToSettingsAction]; | |
[useNotificationsController addAction:cancelAction]; | |
[self.window.rootViewController presentViewController:useNotificationsController animated:true completion:nil]; | |
NSLog(@"We cannot use notifications because the user has denied permissions"); | |
} | |
break; | |
} | |
}]; | |
} else if ((systemVersion < 10) || (systemVersion >= 8)) { | |
UIUserNotificationType types = (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | | |
UIUserNotificationTypeAlert); | |
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; | |
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; | |
} else { | |
NSLog(@"We cannot handle iOS 7 or lower in this example. Contact [email protected]"); | |
} | |
return YES; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Make sure to include this at the top of AppDelegate.swift | |
import UserNotifications | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
// Override point for customization after application launch. | |
// We want to check Notification Settings on launch. | |
// First we must determine your iOS type: | |
// Note this will only work for iOS 8 and up, if you require iOS 7 notifications then | |
// contact [email protected] with your request | |
if #available(iOS 10, *) { | |
UNUserNotificationCenter.current().getNotificationSettings { (settings) in | |
switch settings.authorizationStatus { | |
case .notDetermined: | |
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: { (granted, error) in | |
// You might want to remove this, or handle errors differently in production | |
assert(error == nil) | |
if granted { | |
UIApplication.shared.registerForRemoteNotifications() | |
} | |
}) | |
case .authorized: | |
UIApplication.shared.registerForRemoteNotifications() | |
case .denied: | |
let useNotificationsAlertController = UIAlertController(title: "Turn on notifications", message: "This app needs notifications turned on for the best user experience", preferredStyle: .alert) | |
let goToSettingsAction = UIAlertAction(title: "Go to settings", style: .default, handler: { (action) in | |
}) | |
let cancelAction = UIAlertAction(title: "Cancel", style: .default) | |
useNotificationsAlertController.addAction(goToSettingsAction) | |
useNotificationsAlertController.addAction(cancelAction) | |
self.window?.rootViewController?.present(useNotificationsAlertController, animated: true) | |
} | |
} | |
} else if #available(iOS 8, *) { | |
let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil) | |
UIApplication.shared.registerUserNotificationSettings(settings) | |
} else { | |
print("We cannot handle iOS 7 or lower in this example. Contact [email protected]") | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment