jjzjj

ios - didRegisterForRemoteNotificationsWithDeviceToken 在 ios 10 上没有被调用

coder 2023-09-06 原文

我已经在 appDelegate 中编写了这段代码

import UserNotifications
var registerId = String()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.registerForPushNotifications(application)
    return true   
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
    var tokenString = ""
    for i in 0..<deviceToken.length {
        tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
    }
    registerId = tokenString
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}
func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
            if (granted)
            {
               application.registerForRemoteNotifications()
            }
            else{
                registerId = "111111"
                //Do stuff if unsuccessful...
            }
        })
    }

    else{
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}
}

使用此代码,我能够在 ios 版本 < 10="" 中获取设备="" token="" ,但对于="" ios="" 10="" 及更高版本,不会调用="">

最佳答案

最后一件事,添加框架:

import UserNotifications

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
    registerForPushNotifications(application)
...
}


    func registerForPushNotifications(application: UIApplication) {

    if #available(iOS 10.0, *){
        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
            if (granted)
            {
                UIApplication.shared.registerForRemoteNotifications()
            }
            else{
                //Do stuff if unsuccessful...
            }
        })
    }
    else { //If user is not on iOS 10 use the old methods we've been using
        let notificationSettings = UIUserNotificationSettings(
            types: [.badge, .sound, .alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)

    }

}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

 }
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    //Handle the notification
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    //Handle the notification
}

}

关于ios - didRegisterForRemoteNotificationsWithDeviceToken 在 ios 10 上没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40991477/

有关ios - didRegisterForRemoteNotificationsWithDeviceToken 在 ios 10 上没有被调用的更多相关文章

随机推荐