2016-10-12 11 views
2

私はFCMアプリケーションサーバーを実装しており、自分のデータベース(PHPで書かれたサーバー)にデバイス登録トークンIDを格納したかったのです。iOSからPHPサーバーへのPOSTデータは常にNULLです - iOS 10、Swift 3、php

私はデバイスIDトークンのデータが常にnullであることを認識しています。誰かが適切にトークンをデータベースに格納するように指示できますか?

本当にありがとうと思います。

親切に以下のコード/スクリーンショットを参照してください - AppDelegate.swift

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
    var token = "" 

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

    print("Registration succeeded!") 
    print("Token: ", token) 
    Callquery(token) 

} 

AppDelegate.swift(サーバ側スクリプトにPOSTリクエストを送信Callquery法)

func Callquery(_ token: String) 
{ 

    // append parameter to oneDictionary 
    let tokenString = ["token": token] as [String: Any] 

    // create the request 
    var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php")!) 

    // set the method as POST 
    request.httpMethod = "POST" 

    // append the paramter to body 
    request.httpBody = try! JSONSerialization.data(withJSONObject: tokenString, options: []) 

    // create the session 
    URLSession.shared.dataTask(with:request, completionHandler: {(data, response, error) in 
     if error != nil { 
      print("There was error during datatask session") 
      print(error) 
     } else { 
      do { 
       guard let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] else { return } 

       guard let errors = json?["errors"] as? [[String: Any]] else { return } 
       if errors.count > 0 { 
        // show error 
        print("There is an error during parse JSON datatask") 
        return 
       } else { 
        // show confirmation 
        print("datatask with JSON format performed successfully") 
       } 
      } 
     } 
     print(request) 
    }).resume() 
} 

サービスサイドスクリプト(regis terToken.php):実際のデバイスでアプリケーションを実行

<?php 
include 'config.php'; 

$token = $_POST['token']; 

$sql = "INSERT INTO users (email,device_id) VALUES ('email','$token')"; 

mysqli_query($conn, $sql); 



mysqli_close($conn); 
?> 

は、以下のようにログインします - enter image description here

データベースのユーザーテーブルを(デバイスIDは常に何もしています): - enter image description here

ユーザー表構造: - enter image description here

tokenString: - 私は以下のように代わりにPOSTメソッドのGETメソッドを使用してこの問題を解決していた enter image description here

+0

ごめんバディは道:) –

+0

を知らない

$token = $_GET['token']; 
aznelite89

+0

によっておかげで私はreslut @if tokenString –

答えて

1

: -

Appdelegate.swift:

var request = URLRequest(url: URL(string:"https://YourURL.com/admin/registerToken.php?token=\(token)")!) 

サーバー側スクリプト:

私はPHP
+0

すばらしくシンプル –

関連する問題