【发布时间】:2018-03-27 05:53:54
【问题描述】:
在我的入职培训中,我有一个 UIPageViewController,最后包含一个“入门”屏幕,用于授权通知。用户将点击一个标有“启用通知”的按钮,然后会出现通知权限对话框。我该如何做到这一点?
【问题讨论】:
标签: ios objective-c permissions notifications apple-push-notifications
在我的入职培训中,我有一个 UIPageViewController,最后包含一个“入门”屏幕,用于授权通知。用户将点击一个标有“启用通知”的按钮,然后会出现通知权限对话框。我该如何做到这一点?
【问题讨论】:
标签: ios objective-c permissions notifications apple-push-notifications
你可以放:
Objective-C
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// Enable or disable features based on authorization.
}];
[[UIApplication sharedApplication] registerForRemoteNotifications]; // you can also set here for local notification.
斯威夫特
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
UIApplication.shared.registerForRemoteNotifications() // you can also set here for local notification.
在您的IBAction 中。
请记住在你有IBAction的文件中添加import UserNotifications for Swift或#import <UserNotifications/UserNotifications.h> for Objective-C,并确保Push Notification是在target - Capabilities - Push notification 下激活。
【讨论】:
目标-C:
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
【讨论】:
UIUserNotificationSettings 在 iOS 10 中已弃用。您应该改用 UNNotificationSettings。 developer.apple.com/documentation/uikit/…