2015-03-02 108 views
1

這是我已經把AppDelegate中用於推送通知註冊代碼:奇怪的行爲適用於iOS推送通知註冊時

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

func application(application:UIApplication!, didRegisterForUserNotificationSettings notificationSettings:UIUserNotificationSettings) { 
    println("[AppDelegate][didRegisterForUserNotificationSettings]") 
    UIApplication.sharedApplication().registerForRemoteNotifications() 
} 

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    println("[AppDelegate][didFailToRegisterForRemoteNotificationsWithError]") 
    println(error) 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    println("[AppDelegate][didRegisterForRemoteNotificationsWithDeviceToken]") 
    println(deviceToken) 
} 

當然,當我經過的Xcode 6.1.1發佈在我的iPhone應用程序沒有任何反應,我沒有看到我的println輸出,但是當我離開應用程序並轉至設置 - >通知時,我發現爲我的應用程序啓用了推送通知。

由於didRegisterForRemoteNotificationsWithDeviceToken不叫,我沒有得到deviceToken

我在做什麼錯?

回答

0

也許問題在於,在應用完成啓動時調用的應用委託方法返回true後調用registerForRemoteNotifications()。試試這個:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    let notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound 
    let settings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil) 

    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 

    return true 
} 
0

我去手機設置 - >去一般 - >通知 - >選擇您的應用程序 - >的通知中心 - >將其更改爲10!改變它後,我開始獲取設備令牌數據!