iOS 8.3 获取不到 DeviceToken

Zero__One 发布于 2015/05/26 13:37
阅读 1K+
收藏 0

我这边折腾已经几天了,  一直没法解决一个JB问题.

证书应该木有问题,  大概10来编重新做证书了.

用Swift写的, 

didRegisterForRemoteNotificationsWithDeviceToken 方法一直没有被调用..

代码:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        var types: UIUserNotificationType = UIUserNotificationType.Badge |
            UIUserNotificationType.Alert |
            UIUserNotificationType.Sound
        var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
        
        application.registerUserNotificationSettings(settings)
        application.registerForRemoteNotifications()
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        
    }

    func applicationDidEnterBackground(application: UIApplication) {
        
    }

    func applicationWillEnterForeground(application: UIApplication) {
        
    }

    func applicationDidBecomeActive(application: UIApplication) {
        
    }

    func applicationWillTerminate(application: UIApplication) {
        
    }
    
    func application(application: UIApplication , didReceiveLocalNotification notification: UILocalNotification ) {
        var alertView = UIAlertView (title: " 系统本地通知 " , message: notification.alertBody , delegate: nil , cancelButtonTitle: " 返回 " )
        alertView.show ()
    }

    //before 8.0
    func application(application: UIApplication , didReceiveRemoteNotification userInfo: [ NSObject : AnyObject ]) {
        let notif    = userInfo as NSDictionary
        let apsDic   = notif.objectForKey("aps") as! NSDictionary
        let alertDic = apsDic.objectForKey("alert") as! String
        var alertView = UIAlertView (title: " 系统本地通知 " , message: alertDic, delegate: nil , cancelButtonTitle: " 返回 " )
        alertView.show ()
    }
    
    //after 8.0
    func application(application: UIApplication , didReceiveRemoteNotification userInfo: [ NSObject : AnyObject ], fetchCompletionHandler completionHandler: ( UIBackgroundFetchResult ) -> Void ) {
        let notif    = userInfo as NSDictionary
        let apsDic   = notif.objectForKey("aps") as! NSDictionary
        let alertDic = apsDic.objectForKey("alert") as! String
        var alertView = UIAlertView (title: " 远程推送通知 " , message: alertDic, delegate: nil , cancelButtonTitle: " 返回 " )
        alertView.show()
    }
    
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
        
        var deviceTokenString: String = (deviceToken.description as NSString)
            .stringByTrimmingCharactersInSet( characterSet)
            .stringByReplacingOccurrencesOfString(" ", withString: "") as String
        
        println("didRegisterForRemoteNotificationsWithDeviceToken \(deviceTokenString)")
    }
    
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        println("didFailToRegisterForRemoteNotificationsWithError: \(error)")
    }

    func application(application: UIApplication , didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings ) {
        println("Push notifications called didRegisterUserNotificationSettings")
        if (notificationSettings.types != UIUserNotificationType.None) {
            println("didRegisterUser")
            application.registerForRemoteNotifications()
        }
    }
    
    func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [NSObject : AnyObject], completionHandler: () -> Void){
        if(identifier == "declineAction"){
            println("declineAction")
        }else if(identifier == "answerAction"){
            println("answerAction")
        }
    }
}





加载中
0
f
fjh658
系统时间  是不是自动设置啊?
阿斯顿rthj
哥们 你是怎么解决的啊? 不妨告诉你,我现在也是碰到这个问题了。。。求解答
Zero__One
Zero__One
是自动设置的, 但是问题现在已经解决了, 莫名其妙的可以获取了.. 可能是证书的问题, 具体我也不知道了. 还是谢谢你的回答!
返回顶部
顶部