只能收到的設備標識上的每個發射如果當用戶/授予您的應用程序權限推送通知。此外,這通常可以在AppDelegate中輕鬆捕獲。 (推薦更多的,因爲你可以,如果你啓用了您的應用程序利用本地加密)
所以這裏我舉的例子,我保存到兩個UserDefaults(較少推薦),核心數據。
您請求授權,像這樣,對於應用其目的是要在iOS上10 +工作並請求推送通知在發射..:
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
notificationHandler(application)
return true
}
func notificationHandler(_ application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings(){ (settings) in
switch settings.authorizationStatus {
case .authorized:
print("Authorized Push Notifications by User")
self.registerPushNotifications()
case .denied:
print("show user a view explaining why it's better to enable")
self.registerPushNotifications()
case .notDetermined:
self.requestPushNotifications(center: center, { (granted) in
if granted {
self.registerPushNotifications()
return
}
print("User did not grant Remote Notifications Authorizations")
})
}
}
} else {
print("App does not meet minimum OS Requirements")
return
}
}
fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) {
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if error != nil {
print("Could not request Authorization for Notifications - Yet is undetermined")
} else {
result(granted)
}
}
}
func registerPushNotifications() {
if #available(iOS 10.0, *) {
UIApplication.shared.registerForRemoteNotifications()
} else {
print("App does not meet base OS requirements")
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken")
coreDataManager.saveDeviceToken(deviceTokenString)
}
}
最後,如果你只想在這個請求在您的應用程序的其他時刻,你可以擴展NSObject
,因爲它是既AppDelegate
和UIViewController
的超類(也就是根類的所有控制器型類的反正。
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
notificationHandler(application)
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken")
coreDataManager.saveDeviceToken(deviceTokenString)
}
}
extension NSObject {
func notificationHandler(_ application: UIApplication) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.getNotificationSettings(){ (settings) in
switch settings.authorizationStatus {
case .authorized:
print("Authorized Push Notifications by User")
self.registerPushNotifications()
case .denied:
print("show user a view explaining why it's better to enable")
self.registerPushNotifications()
case .notDetermined:
self.requestPushNotifications(center: center, { (granted) in
if granted {
self.registerPushNotifications()
return
}
print("User did not grant Remote Notifications Authorizations")
})
}
}
} else {
print("App does not meet minimum OS Requirements")
return
}
}
fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) {
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
if error != nil {
print("Could not request Authorization for Notifications - Yet is undetermined")
} else {
result(granted)
}
}
}
func registerPushNotifications() {
if #available(iOS 10.0, *) {
UIApplication.shared.registerForRemoteNotifications()
} else {
print("App does not meet base OS requirements")
}
}
}
然後,您可以撥打這裏面一個按鈕的鏈接的方法如下:
class SomeController : UIViewController {
override viewDidLoad() {
super.viewDidLoad()
}
func requestNotificationsAuthorization() {
notificationHandler(UIApplication.current)
}
}
可以澄清你想達到的結果嗎? –
不要轉發問題。我將你的[上一個問題](https://stackoverflow.com/questions/45459888/how-to-present-apns-token-in-view-controller)標記爲重複。在鏈接的問題有很多的方式來獲得設備令牌作爲字符串,這個作品甚至可能在斯威夫特4.除此之外,目前還不清楚你要完成的任務。 – vadian
您可以將訪問令牌存儲在用戶默認值中。 – Jaydeep