如何在AppDelegate之外调用“application didRegisterForRemoteNotificationsWithDeviceToken”

时间:2015-12-16 18:20:46

标签: ios swift push-notification

我想在Swift中使用POST请求将我的用户tokens id保存到我的数据库中。

我使用此功能

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let types: UIUserNotificationType = [.Alert, .Badge, .Sound]

    let settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )

    application.registerUserNotificationSettings( settings )
    application.registerForRemoteNotifications()

    return true
}

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

    let characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )

    let deviceTokenString: String = ( deviceToken.description as NSString )
        .stringByTrimmingCharactersInSet( characterSet )
        .stringByReplacingOccurrencesOfString( " ", withString: "" ) as String

    print("token=" + deviceTokenString )

}

问题=如何在AppDelegate类之外调用didRegisterForRemoteNotificationsWithDeviceToken?

1 个答案:

答案 0 :(得分:1)

您可以通过调用

触发应用委托代理didRegisterForRemoteNotificationsWithDeviceToken委托meathod在您应用的其他位置调用
UIApplication.sharedApplication().registerForRemoteNotifications()

但是你没有直接调用这种方法。