Http post 请求在数据库中存储令牌
Http post request for store token in database
我正在 wordpress 中实现推送通知,除了将令牌存储在数据库中外,一切都对我有用。
我在AppDelegate.swift
中有以下功能
// send token
func SendToken(_ token: String)
{
//info device
// append parameter to oneDictionary
let tokenString = ["token": token] as [String: Any]
// create the request
let stringUrl = "http://example.com/wp/wp-json/apnwp/register?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)"
let stringUrlEncoded = stringUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let myUrl = URL(string: stringUrlEncoded!)
var request = URLRequest(url: myUrl!)
// 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 }
//print(json as Any)
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()
}
//end send token
我使用的插件文档说明如下:
URL structure:
http://yourwordpresssite/wp-json/apnwp/register
Method: GET
Parameters:
device_token (string): token given by APNs or FCM identifying the device, often called device ID.
os_type (string): operating system. It must be ios or android (case sensitive).
user_email_id (string, optional): the user email address which is login from your mobile app.
Examples:
http://yourwordpresssite/wp-json/apnwp/register?os_type=android&user_email_id=androidmobile@40test.com&device_token=1234567890
在AppDelegate.swift中我调用函数:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("New token is: \(token)")
SendToken(token)
}
但是把数据存到数据库里是不行的,但是如果我把URL(//yourwordpresssite/wp-json/apnwp/register?os_type=android&user_email_id=iosdmobile@40test.com&device_token=1234567890
)放在浏览器里如果是存到数据库里的话
如果有任何帮助,我将不胜感激。
查看 插件文档 ,我在您的代码中发现了一些问题:
1.方法:GET
你的 http 方法是 GET,不是 POST
2。 httpBody 作为 ["token": 令牌]
您已经在 urlRequest
的末尾附加了令牌作为 ?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)
。
即使使用 request.httpBody = nil
调用 GET 请求,您的所有参数都应附加在 urlRequest
中。
3。使用 [ ]
而不是 .allowFragments
解码 JSON 对象
我不确定,但如果您无法解析数据,您应该使用 []
而不是 .allowFragments
guard let json = try? JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] else { return }
Try updating your code as:
func SendToken(_ token: String) {
// Create reuest
let stringUrl = "http://example.com/wp/wp-json/apnwp/register?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)"
let stringUrlEncoded = stringUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let myUrl = URL(string: stringUrlEncoded!)
var request = URLRequest(url: myUrl!)
// set the method as GET
request.httpMethod = "GET"
// append the paramter to body
request.httpBody = nil
// 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: []) 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()
}
我正在 wordpress 中实现推送通知,除了将令牌存储在数据库中外,一切都对我有用。
我在AppDelegate.swift
中有以下功能// send token
func SendToken(_ token: String)
{
//info device
// append parameter to oneDictionary
let tokenString = ["token": token] as [String: Any]
// create the request
let stringUrl = "http://example.com/wp/wp-json/apnwp/register?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)"
let stringUrlEncoded = stringUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let myUrl = URL(string: stringUrlEncoded!)
var request = URLRequest(url: myUrl!)
// 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 }
//print(json as Any)
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()
}
//end send token
我使用的插件文档说明如下:
URL structure:
http://yourwordpresssite/wp-json/apnwp/register
Method: GET
Parameters:
device_token (string): token given by APNs or FCM identifying the device, often called device ID.
os_type (string): operating system. It must be ios or android (case sensitive).
user_email_id (string, optional): the user email address which is login from your mobile app.
Examples:
http://yourwordpresssite/wp-json/apnwp/register?os_type=android&user_email_id=androidmobile@40test.com&device_token=1234567890
在AppDelegate.swift中我调用函数:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
var token = ""
for i in 0..<deviceToken.count {
token = token + String(format: "%02.2hhx", arguments: [deviceToken[i]])
}
print("New token is: \(token)")
SendToken(token)
}
但是把数据存到数据库里是不行的,但是如果我把URL(//yourwordpresssite/wp-json/apnwp/register?os_type=android&user_email_id=iosdmobile@40test.com&device_token=1234567890
)放在浏览器里如果是存到数据库里的话
如果有任何帮助,我将不胜感激。
查看 插件文档 ,我在您的代码中发现了一些问题:
1.方法:GET
你的 http 方法是 GET,不是 POST
2。 httpBody 作为 ["token": 令牌]
您已经在 urlRequest
的末尾附加了令牌作为 ?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)
。
即使使用 request.httpBody = nil
调用 GET 请求,您的所有参数都应附加在 urlRequest
中。
3。使用 [ ]
而不是 .allowFragments
我不确定,但如果您无法解析数据,您应该使用 []
而不是 .allowFragments
guard let json = try? JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any] else { return }
Try updating your code as:
func SendToken(_ token: String) {
// Create reuest
let stringUrl = "http://example.com/wp/wp-json/apnwp/register?os_type=ios&user_email_id=pushx@40test.com&device_token=\(token)"
let stringUrlEncoded = stringUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let myUrl = URL(string: stringUrlEncoded!)
var request = URLRequest(url: myUrl!)
// set the method as GET
request.httpMethod = "GET"
// append the paramter to body
request.httpBody = nil
// 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: []) 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()
}