2017-02-20 22 views
0

私はこのコードをアプリの読み込み時にプッシュ通知に登録するが、何も起こらなかった。iOSでプッシュ通知を正しく登録する方法は?

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     singleton.deviceToken = nil; 
     application.registerForRemoteNotifications() 
     return true 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     var token = deviceToken.description.trimmingCharacters(in: CharacterSet.init(charactersIn:"<>")); 
     token = token.replacingOccurrences(of: " ", with: ""); 
     singleton.deviceToken = token; 
    } 

    func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) { 
     if notificationSettings.types == .none { application.registerForRemoteNotifications() } 
    } 

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 
     print("Cannot register for push notification. Repeating.") 
     application.registerForRemoteNotifications(); 
    } 
  • 私は上記のすべてのメソッドにブレークポイントを設定しているが、唯一の方法は、唯一のapplication(application:didFinishLaunchingWithOptions:)で呼び出されます。他のすべては決して呼び出されません。
  • 私のテストデバイス上のアプリケーションは、プッシュ通知を表示する要求を絶対に表示しません。
  • 私はターゲットにプッシュ通知を表示する機能を既に要求しています。それは言う:[✓]アプリケーションIDにプッシュ通知を追加し、[✓]エンタイトルメントファイルにプッシュ通知エンタイトルメントを追加します。コンソールに印刷

  • 何もありません。

どうしたのですか?そしてこれを修正する方法は?少なくとも私はプッシュ通知に登録するのに失敗した場合、Cannot register for push notification. Repeating.がコンソールに繰り返し表示されることを期待しています。しかし、コンソールには何もありません。

PS:Xcodeを8.2.1にアップデートしました。しかし、私は8.2.1にアップグレードするまで私の現在のプロジェクトでプッシュ通知を試したことはありませんでしたので、古いXcodeで作業している場合にプッシュ通知が機能するかどうかわかりません。

+0

でUNUserNotificationCenterDelegateを追加するには、プロジェクト設定の機能]タブで通知をプッシュ有効にしましたか? – Vakas

+0

@Vakasはい私は持っています。 –

+0

プッシュ通知用の開発者ポータルで必要な設定ですか? – Vakas

答えて

0
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    self.registerForRemoteNotification(application:application) // call method to register push notification 
    return true 
} 

//MARK : Push Notification 
// add this code in AppDelegate.swift outside of classs 
import UserNotifications 
extension AppDelegate:UNUserNotificationCenterDelegate { 
    func registerForRemoteNotification(application: UIApplication) { 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.delegate = self 
      center.requestAuthorization(options: [.alert,.sound,.badge], completionHandler: { (complete, error) in 
       if let err = error { 
        print(err.localizedDescription) 
       } else { 
        UIApplication.shared.registerForRemoteNotifications() 
       } 
      }) 
     } 
     else { 
      let types:UIUserNotificationType = ([.alert, .sound, .badge]) 
      let settings:UIUserNotificationSettings = UIUserNotificationSettings(types:types, categories:nil) 
      application.registerUserNotificationSettings(settings) 
      application.registerForRemoteNotifications() 
     } 
    } 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     print("User Info = ",response.notification.request.content.userInfo) 
     completionHandler() 

    } 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     print("User Info = ",notification.request.content.userInfo) 
     completionHandler([.alert,.sound,.badge]) 
    } 

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

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { 
     print("Fail to register for notification : \(error.localizedDescription)") 
    } 

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

     if UIApplication.shared.applicationState == .active { 
      if let dictAps = userInfo["aps"] as? NSDictionary { 
       if let message = dictAps["alert"] as? String { 
        print(message) // show 

       } 
      } 
     } 

    } 
} 
+0

さて、待って、私はこれを試してみるとあなたに戻ってきます。ありがとう! –

0

輸入UserNotifications

フレームワークとAppDelegate.swift 要求ユーザ権限

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    registerForRemoteNotification() 
    return true 
} 

func registerForRemoteNotification() { 
    if #available(iOS 10.0, *) { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in 
      if error == nil{ 
       UIApplication.shared.registerForRemoteNotifications() 
      } 
     } 
    } 
    else { 
      UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil)) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
} 

//Getting device token 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) 
print(deviceTokenString) 


} 
//In case of error 
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 

    print("i am not available in simulator \(error)") 

} 
//In case if you need to know the permissions granted 
UNUserNotificationCenter.current().getNotificationSettings(){ (setttings) in 

     switch setttings.soundSetting{ 
     case .enabled: 

      print("enabled sound setting") 

     case .disabled: 

      print("setting has been disabled") 

     case .notSupported: 
      print("something vital went wrong here") 
     } 
    } 
+0

さて、お待ちください、私はこれを試してみるとあなたに戻ってきます。ありがとう! –

関連する問題