为什么我不能为push notfication apns获取deviceToken?

时间:2016-04-30 13:48:02

标签: ios swift apple-push-notifications

这是我的AllDelegate.swift代码

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {   
        var window: UIWindow?
        var settings: UIUserNotificationSettings?     
        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
            UIApplication.sharedApplication().registerUserNotificationSettings(settings!)
            UIApplication.sharedApplication().registerForRemoteNotifications()
            return true
        }
        .
.
.
.
.
.
.

        func application(application: UIApplication, didRegisterForRomoteNotificationWithDeviceToken deviceToken: NSData){

            print("Got token data! \(deviceToken)")


            let characterSet: NSCharacterSet = NSCharacterSet(charactersInString: "<>")
            let DeviveTokenString: String = (deviceToken.description as NSString)
                .stringByTrimmingCharactersInSet(characterSet)
                .stringByReplacingOccurrencesOfString(" ", withString: "") as String


            let pushBadge = settings!.types.contains(.Badge) ? "enabled" : "dissable"
            let pushAlert = settings!.types.contains(.Alert) ? "enabled" : "dissable"
            let pushSound = settings!.types.contains(.Sound) ? "enabled" : "dissable"

            let myDevice = UIDevice();
            let deviceName = myDevice.name
            let deviceModel = myDevice.model
            let systemVersion = myDevice.systemVersion
            let deviceId = myDevice.identifierForVendor!.UUIDString


            var appName:String?
            if let appDisplayName =
                NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleDisplayName"){
                appName = appDisplayName as? String
            } else {
                appName = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBudleName")
                    as? String
            }

            let appVersion
                = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBudleVersion") as? String

            let myUrl = NSURL(string: "http://rb.cafe/api/apns.php");
            let request = NSMutableURLRequest(URL:myUrl!);
            request.HTTPMethod = "POST";


            let PostString = "task=register&appame=\(appName!)&appversion=\(appVersion!)&deviceuid=\(deviceId)&devicetoken=\(DeviveTokenString)&devicename=\(deviceName)&devicemodel=\(deviceModel)&deviceversion=\(systemVersion)&pushbadge=\(pushBadge)&pushalert\(pushAlert)&pushound=\(pushSound)"

            request.HTTPBody = PostString.dataUsingEncoding(NSUTF8StringEncoding);


            let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
                data, response, error in

                if error != nil{
                    print("error\(error)")
                    return
                }

                let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print ("response \(responseString!)")
            }

            task.resume()

        }

        func application(applicatio:UIApplication, didFailToRegisterFoRemoteNoticationWithError error: NSError){

            print("Error to got data! \(error)")

        }

        func application(application: UIApplication, didReceiveForRomoteNotificationWithDeviceToken userInfo: [NSObject: AnyObject]){      
            print("Detail! \(userInfo)")        
        }     
    }

我认为阅读

有问题
func application(application: UIApplication, didRegisterForRomoteNotificationWithDeviceToken deviceToken: NSData)

我从https://www.youtube.com/watch?v=aOVn4kuQC6Y&list=PLdW9lrB9HDw0YBnnRIc0UeysAcuF73mGm&index=10

得到这个

0 个答案:

没有答案