swift iOS 10上的UserNotifications
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift iOS 10上的UserNotifications相关的知识,希望对你有一定的参考价值。
// 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;
}
// 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
}
// 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 support@pubnub.com 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 support@pubnub.com");
}
return YES;
}
// 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 support@pubnub.com 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 support@pubnub.com")
}
return true
}
以上是关于swift iOS 10上的UserNotifications的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 3 中检测 iOS9 和 iOS10 上的屏幕锁定或主页按钮按下
iOS 10.2 上的 Swift 3:如何从麦克风订阅单个样本?需要实时处理音频
Xcode 8.3 Swift 3 FCM 通知上的 Firebase 问题 iOS 10.3 不起作用
Swift 3 - iOS 11.2上的UICollectionview问题