以下のコードでプッシュ通知を送る際のトークンを取得しようとしていますが
Could not signal service com.apple.WebKit.Networking: 113: Could not find specified service
と表示されます。どうすればトークンを取得できるでしょうか?
swift
1import UIKit 2import UserNotifications 3 4@UIApplicationMain 5class AppDelegate: UIResponder, UIApplicationDelegate { 6 7 var window: UIWindow? 8 9 10 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 11 12 if #available(iOS 10.0, *) { 13 // iOS 10 14 let center = UNUserNotificationCenter.current() 15 center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, error) in 16 if error != nil { 17 return 18 } 19 20 if granted { 21 debugPrint("通知許可") 22 } else { 23 debugPrint("通知拒否") 24 } 25 }) 26 27 } else { 28 // iOS 9 29 let settings = UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil) 30 UIApplication.shared.registerUserNotificationSettings(settings) 31 } 32 33 return true 34 35 } 36 37 //APNsに登録が成功すると呼ばれる 38 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 39 40 print("deviceToken = (deviceToken)") 41 42 let characterSet: NSCharacterSet = NSCharacterSet( charactersIn: "<>" ) 43 44 let deviceTokenString: String = ( deviceToken.description as NSString ) 45 .trimmingCharacters( in: characterSet as CharacterSet ) 46 .replacingOccurrences( of: " ", with: "" ) as String 47 48 print( deviceTokenString ) 49 50 51 let server = "" 52 let request = NSMutableURLRequest(url: NSURL(string: server)! as URL) 53 request.httpMethod = "POST" 54 55 let token = "token=(deviceTokenString)" 56 57 request.httpBody = token.data(using: String.Encoding.utf8) 58 59 var response: URLResponse? 60 61 let resultData = try! NSURLConnection.sendSynchronousRequest(request as URLRequest, returning: &response) 62 63 let myData:NSString = NSString(data: resultData, encoding: String.Encoding.utf8.rawValue)! 64 65 66 print(myData) 67 68 } 69 70 func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 71 print("error: (error)") 72 } 73 74 func applicationWillResignActive(_ application: UIApplication) { 75 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 76 // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 77 } 78 79 func applicationDidEnterBackground(_ application: UIApplication) { 80 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 81 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 82 } 83 84 func applicationWillEnterForeground(_ application: UIApplication) { 85 // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 86 } 87 88 func applicationDidBecomeActive(_ application: UIApplication) { 89 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 90 } 91 92 func applicationWillTerminate(_ application: UIApplication) { 93 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 94 } 95 96 //アプリケーションが起動している状態もしくは通知から起動した場合に呼ばれる 97 private func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 98 print ("application:didReceiveRemoteNotification: " + userInfo.description); 99 } 100 101}
エラーから察するにトークンは取得できていて、そのトークンをどこかにPOSTする時点でエラーが出ていませんか?
あなたの回答
tips
プレビュー