iphone - 如何在 APNS iPhone 应用程序的 php 服务器中保存所有 DeviceToken?

标签 iphone ios apple-push-notifications apns-php

正在将 APNS 集成到我的 iPhone 应用程序中。我已经浏览了苹果公司提供的苹果推送通知文档。我对服务器端有疑问。请在下面找到我的疑问,

  1. Apple said to create a server from below steps,

    Installing the SSL Certificate and Key on the Server:
    You should install the SSL distribution certificate and private cryptographic key you obtained earlier on the server computer on which the provider code runs and from which it connects with the sandbox or production versions of APNs. To do so, complete the following steps:
    1.1.Open Keychain Access utility and click the My Certificates category in the left pane.
    1.2.Find the certificate you want to install and disclose its contents.
    You'll see both a certificate and a private key.
    1.3.Select both the certificate and key, choose File > Export Items, and export them as a Personal Information Exchange (.p12) file.
    1.4.Servers implemented in languages such as Ruby and Perl often are better able to deal with certificates in the Personal Information Exchange format. To convert the certificate to this format, complete the following steps:
         a.In KeyChain Access, select the certificate and choose File > Export Items. Select the Personal Information Exchange (.p12) option, select a save location, and click Save.
         b.Launch the Terminal application and enter the following command after the prompt: openssl pkcs12 -in CertificateName.p12 -out CertificateName.pem -nodes
    1.5.Copy the .pem certificate to the new computer and install it in the appropriate place.

Am clear to create a server (i assume it will be .php server). Am doubt is how we storing all user's DeviceTokens in the server?

2. How to send push notifications to all registered devicetokens?
3. How to send push notifications to a specific user?

您能针对我的问题提出一些建议吗?我从此链接 http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 获得了一个示例 .php 文件。请帮我。提前致谢。

最佳答案

您可以使用以下代码在服务器端获取它:

-(void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);


    NSData *postData = [[NSString stringWithFormat:@"token=%@", deviceToken] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"YOURDOMAIN/FILE.php"]]];

    [request setHTTPMethod:@"POST"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setHTTPBody:postData];

    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
}

只需将您的 token 存储在数据库中($_POST['token'])

要发送通知,您必须执行如下所示的循环:

//getTokens from database
$deviceToken = getDeviceTokens();

foreach($arr AS $key => $val)
    {
        // Create the payload body
        $body['aps'] = array(
                'alert' => 'Puuuuush',
                'sound' => 'vuvzela.wav',
                'badge' => ($badge != (null) ? $badge + 1 : 1)
                );

// Encode the payload as JSON
        $payload = json_encode($body);

        // Build the binary notification
        $msg = chr(0) . pack('n', 32) . pack('H*', $val) . pack('n', strlen($payload)) . $payload;

        // Send it to the server
        $result = fwrite($fp, $msg, strlen($msg));

        if (!$result)
                echo 'Message not delivered' . PHP_EOL;
        else
                echo 'Message successfully delivered' . PHP_EOL;

        $count++;
    }

关于iphone - 如何在 APNS iPhone 应用程序的 php 服务器中保存所有 DeviceToken?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10300423/

相关文章:

iPhone Interface Builder - 将资源移至子目录,现在 IB 看不到它们!

iphone - 是否建议移动浏览器重置 CSS?

iPhone 应用程序升级和移动 xibs/nibs 导致资源过时

ios - Apple Watch 上的图像

ios - 加载自己的 ca 证书时,Swift 2.0 中 SecTrustEvaluate 调用上的 EXC_BAD_ACCESS

ios - 以编程方式生成 APNS 证书

iphone - 如何避免 iOS 上的盗版(通常在移动应用程序中)

ios - 如何获取分组 TableView 的标题 View ?

ios - APNs 远程通知委托(delegate)方法未被调用

php - APNs 消息已发送但未在 iOS 设备上接收