iPhoneからデバイストークンをEC2のget_device_token.phpにPOSTし、device_token.htmlに書き込もうとしているのですが、上手く行きません。
AppDelegate.swift の一部
swift
1 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 2 // APNs 3 UNUserNotificationCenter.current().requestAuthorization( 4 options: [.badge, .alert, .sound]) {(accepted, error) in 5 if accepted { 6 print("Notification access accepted !") 7 8 // デバイストークンを登録 9 UIApplication.shared.registerForRemoteNotifications() 10 } 11 else{ 12 print("Notification access denied.") 13 } 14 } 15 return true 16 } 17 18 // Remote Notification のエラーを受け取る 19 func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { 20 print(error) 21 } 22 23 // Remote Notification の device token を表示 24 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 25 26 var deviceToken = String(format: "%@", deviceToken as CVarArg) as String//トークン原型取得 27 print("DeviceToken = \(deviceToken)") 28 // < > を外す 29 let characterSet: CharacterSet = CharacterSet.init(charactersIn: "<>") 30 deviceToken = deviceToken.trimmingCharacters(in: characterSet) 31 // スペースを除去 32 deviceToken = deviceToken.replacingOccurrences(of: " ", with: "") 33 34 sendToken(token: deviceToken)//送信 35 36 print("deviceToken = \(deviceToken)") 37 } 38 39 // POST 送信 40 func sendToken(token: String){ 41 let serverPhp = "http://ec2-hogehoge.compute.amazonaws.com/get_device_token.php"//サーバのURL 42 let postString = "DeviceToken=" + token 43 44 var request = URLRequest(url: URL(string: serverPhp)!) 45 request.httpMethod = "POST" 46 request.httpBody = postString.data(using: .utf8) 47 48 let task = URLSession.shared.dataTask(with: request, completionHandler: { 49 (data, response, error) in 50 51 if error != nil { 52 print("error") 53 return 54 } 55 print("response: \(response!)") 56 }) 57 task.resume() 58 }
get_device_token.php
php
1<?php 2ob_start(); 3$filename = 'device_token.html'; 4 5if(isset($_POST['DeviceToken'])){ 6$token = $_POST['DeviceToken']; 7file_put_contents( $filename, $token); 8echo "Successfully submitted: " . $token; 9}else{ 10file_put_contents( $filename, "no token"); 11echo "ERROR: "; 12} 13 14ob_end_flush() 15?>
Xcodeのログ
Notification access accepted ! deviceToken = <hogehoge hogehoge hogehoge hogehoge hogehoge hogehoge hogehoge hogehoge> deviceToken = 8hogehogehogehoge response: <NSHTTPURLResponse: 0x17002e4a0> { URL: http://ec2-hoge.compute.amazonaws.com/get_device_token.php } { status code: 200, headers { "Accept-Ranges" = bytes; Connection = "Keep-Alive"; "Content-Length" = 466; Date = "Wed, 22 Mar 2017 03:48:57 GMT"; Etag = "\"1hoge\""; "Keep-Alive" = "timeout=5, max=100"; "Last-Modified" = "Wed, 22 Mar 2017 03:45:02 GMT"; Server = "Apache/2.4.18 (Ubuntu)"; } }
アプリを起動すると、POST通信して、device_token.htmlが作成されると思ったのですが、作成されません。
また、echoがXcodeのログに返ってくると思っていたのですが、それも返ってきません。
ファイルを置き、POSTするだけでは、phpが実行されないのでしょうか?
初歩的なミス・勘違いかもしれませんが、よろしくお願いいたします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/23 04:36
2017/03/23 06:39