在我的入门中,我有一个UIPageViewController
,在末尾包含一个用于授权通知的“入门”屏幕。用户点击标有“启用通知”的按钮,就会出现通知权限对话框。我该如何实现?
最佳答案
您可以输入:
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.
swift
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
里面.
请记住同时添加 import UserNotifications
对于 Swift 或 #import <UserNotifications/UserNotifications.h>
对于 Objective-C 在您有 IBAction
的文件中并确保 Push Notification
在 target
下激活- Capabilities
- Push notification
.
关于ios - 如何在 IBAction 中请求通知权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757966/