好的,我已经尝试解决这个问题大约三周了,我认为我不需要再剃光头了!!!我已尽可能多地解构代码,因此我可以专注于将设备令牌获取到我的数据库中。当我运行代码时,我在我的数据库中得到一个时间戳记录,就是这样,所以我知道它是连接的,但那里什么也没有。
这是我的代码
- (void)application:(UIApplication*)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString *tokenStr = [deviceToken description];
NSString *pushToken = [[[[tokenStr
stringByReplacingOccurrencesOfString:@"<" withString:@""]
stringByReplacingOccurrencesOfString:@">" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""] retain];
// Save the token to server
NSString *urlStr = [NSString stringWithFormat:ServerApiURL];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"&token=%@",
pushToken] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];
[[NSURLConnection alloc] initWithRequest:req delegate:nil];
NSLog(@"%@", pushToken);
NSLog(@"%@", url);
}
这是非常简单的PHP
// initialize connection to database
$db = mysql_connect('localhost', 'xxxxxxxxx', 'xxxxxxxx');
$db_name = "xxxxxxxxxxxx";
mysql_select_db($db_name, $db);
// store incoming data as php variables
error_log(print_r($_POST, true));
$token = $_POST['token'];
// create mysql query
mysql_query("INSERT INTO iPhone (device_token)
VALUES ('$token')");
mysql_query($query);
mysql_close($db);
?>
我在数据库中得到的是时间戳,但在 device_token 下没有任何内容。我将不胜感激。谢谢。