如果您不想使用 Urban Airship 等服务。您将创建一个数据库(可能是 MySQL),然后使用 PHP 等语言创建一个文件,该文件允许您发布设备令牌,然后将其放入数据库中。
在 PHP 中,这看起来像
<?php
$connection = mysql_connect("localhost","username","password");
if (!$connection){
die('Error: ' . mysql_error());
}
mysql_select_db("my_database", $connection);
mysql_query("INSERT INTO tokens (token)
VALUES ('$_POST[token]')");
mysql_close($connection);
?>
在 iOS 端,您将使用以下代码:
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *urlString;
NSURL *url;
NSMutableURLRequest *request;
NSString *postString;
urlString = @"http://yoururl.com/apns/registerDevice";
url = [NSURL URLWithString:urlString];
request = [NSMutableURLRequest requestWithURL:url];
postString = [NSString stringWithFormat:@"token=%@", [[NSString alloc] initWithData:deviceToken encoding:NSUTF8StringEncoding];];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
(void)[[NSURLConnection alloc] initWithRequest:request delegate:self];
//Casting this to void makes the warning of unused expression go away.
}