我正在使用苹果通知服务,所以在这个函数中我必须注册每个下载我的应用程序的设备 token ,或者换句话说,当我想发送通知时,我必须将它发送到哪个地址。
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
NSString *str = [NSString stringWithFormat:@"Error: %@",[error localizedDescription]];
NSLog(@"%@",str);}
最佳答案
如果您不想使用 Urban Airship 等服务。您将创建一个数据库(可能是 MySQL),然后使用 PHP 等语言创建一个文件,该文件允许您发布设备 token ,然后将其放入数据库。
在 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.
}
关于iphone - 我必须注册下载我的应用程序的每个设备 token 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11379130/