我在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
)放在浏览器中,如果它存储在数据库中。
我会感激任何帮助,可能会发生什么。
答案 0 :(得分:0)
查看插件文档,我在您的代码中发现了一些问题:
<强> 1。方法:GET
你的http方法是GET,而不是POST
<强> 2。 httpBody为[&#34;令牌&#34;:令牌]
您已将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 }
尝试将代码更新为:
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()
}