私は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);
?>
データベースのユーザーテーブルを(デバイスIDは常に何もしています): -
tokenString: - 私は以下のように代わりにPOSTメソッドのGETメソッドを使用してこの問題を解決していた
ごめんバディは道:) –
を知らない
– aznelite89によっておかげで私はreslut @if tokenString –