iphone - 我必须注册下载我的应用程序的每个设备 token 吗?

标签 iphone ios push-notification

我正在使用苹果通知服务,所以在这个函数中我必须注册每个下载我的应用程序的设备 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/

相关文章:

ios - 如果有任何应用程序在后台运行,请调整大小

iphone - 缩进分组单元格,如 iPhone 联系人

objective-c - XCode 堆快照非对象增加导致内存崩溃

ios - Phonegap应用程序可以在phonegap应用程序中使用,但不能在phonegap构建中使用

iOS8 自动 tableview 高度在第一个单元格上错误

iphone - 苹果推送通知在生产中不起作用

android - 如何显示累积推送通知而不是显示往往同时出现的多个通知?

iPhone 4 的视口(viewport)问题

iphone - 处理 NSNumber 时,NSMutableDictionary 根据对象的地址或值进行查找?

android - 奥利奥通知图标不显示