2016-11-11 16 views
0

私はプッシュ通知を必要とするこの魅力的なアプリを作っています。プッシュ通知機能の登録が呼び出されない

しかし、プッシュトークンを取得しようとすると、すべての必要な機能が実行されないようです。

ユーザーがこのアラートを提示取得ん:

enter image description here

ユーザーが[OK]をヒットすると、私は私のコードをステップ実行していない、すべてが実行されることを参照してください。私のコンソールで

override func viewDidLoad(){ 
    let tapper = UITapGestureRecognizer(target: view, action:#selector(UIView.endEditing)) 
    tapper.cancelsTouchesInView = false 
    view.addGestureRecognizer(tapper) 

    print("gets called") 
    registerForPushNotifications(UIApplication.sharedApplication()) 
} 

func registerForPushNotifications(application: UIApplication) { 
    print("gets called") 
    let notificationSettings = UIUserNotificationSettings(
     forTypes: [.Badge, .Sound, .Alert], categories: nil) 
    application.registerUserNotificationSettings(notificationSettings) 
} 

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) { 
    print("doesn't get called") 
    if notificationSettings.types != .None { 
     application.registerForRemoteNotifications() 
    } 
} 

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    print("doesn't get called") 
    let tokenChars = UnsafePointer<CChar>(deviceToken.bytes) 
    var apnsTokenString = "" 

    for i in 0..<deviceToken.length { 
     apnsTokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]]) 
    } 

    Constant.pushToken = apnsTokenString 

    print("Device Token:", Constant.pushToken) 
} 

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
    debugPrint("Error Registering Remote Notification") 
} 

次のように印刷されます。

が呼び出されます

はない、必要なすべての機能が呼び出されることを意味

呼び出されます。私は間違って何をしていますか?

+0

登録していませんでした.RegisterForRemoteNotificationsWithDeviceToken func right?このリンクを参照してください - http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10/39506524#39506524 –

+0

いいえ、その1つは呼ばれません。それはまさに私が呼び出す必要がある機能です。 –

+0

大丈夫、このリンクに記載されている手順に従ってください - http://stackoverflow.com/questions/39490605/push-notification-issue-with-ios-10/39506524#39506524 –

答えて

1

このコードを使用し、問題はあなたの実際のアプリのデリゲートクラスでUIApplicationDelegateメソッドを配置する必要があるということです、ビューコントローラクラスではありません。これらのメソッドを適切なクラスに移動するだけで動作します。

+0

3つ下の関数をAppDelegate.swiftクラスに移動することを意味しますか? –

+1

はい、それは正確です。 – rmaddy

-1

ちょうどあなたがあなたの質問に掲載のすべてのコードは、あなたのビューコントローラクラスであると仮定すると、

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


    print("gets called") 
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 

      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
} 



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(deviceTokenString) 
    } 
+0

私のために働かない。 2番目の関数は呼び出されません。 –

+0

あなたはApp Delegateを呼び出す必要があります –

関連する問題