我一直在取得进展,但在如何使用服务器端的 PHP 和我的 iOS 应用程序上的 Swift 代码将我的用户的设备令牌放入 MySQL 数据库方面停留了几天。它成功注册了推送通知,但不会发送到我的服务器。代码片段如下。此外,我还想发送设备的 IP 以匹配我应用的当前用户。任何帮助表示赞赏!
斯威夫特代码:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// 1. Convert device token to string
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
// 2. Print device token to use for PNs payloads
print("Successfully registered for notifications!")
print("Device Token: \(token)")
URLRequest(url: URL(string:"https://MY_URL.com/registerPushApple.php?key=MY_KEY&token=\(token)")!)
}
PHP代码:
//Put in database
date_default_timezone_set('America/New_York');
$date = date("Y-m-d");
$device_ip = $_GET['device_ip'];
$device_token = $_GET['token'];
$key = $_GET['key'];
if($key == "MY_KEY") {
// Insert Info into Database
$mysqli->query("INSERT INTO device_info (date, device_ip, device_token) VALUES ('$date', '$device_ip', '$device_token')");
} else {
die();
}
// close connection
$mysqli->close();
撒科打诨