2012-04-24 51 views
2

我在我的iPhone应用程序集成APNS。我已经阅读了Apple提供的Apple推送通知文档。我对服务器端有疑问。请在下面找到了我的怀疑,如何保存APNS的iPhone应用程序在PHP服务器的所有DeviceTokens?

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文件。请帮帮我。提前致谢。

回答

4

您可以使用此代码得到它的服务器端:

-(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]; 
} 

只是存储在DB你的令牌($ _ POST [“令牌”])

要送你必须做一个循环的通知,其看起来是这样的:

//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++; 
    } 
+0

你好丹尼尔。感谢您的杰出答案。我还有一个疑问可以请你澄清。 1.我们如何挑选特定用户的设备和发送通知? 2.苹果认为.pem文件将成为服务器。一些教程认为服务​​器将在PHP?你可以pelase澄清这些?提前致谢。 – 2012-04-25 09:27:42

+0

如果我们在将作为服务器的本地机器中使用.pem文件存储。因此,我们如何在.pem文件中编写代码(您的代码)。我没有开始等待老板证书的过程。我仍然对APNS有很多疑问。你能帮我么。谢谢。 – 2012-04-25 09:36:02

+0

我正在尝试相同的东西atm。我想发送个性化的推送通知。我的想法是,我会将设备标记存储到本地存储中,并通过我的PhoneGap应用程序中的JavaScript获取它们。 下面是.pem文件一个伟大的教程: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 只要按照它一步一步。这对我帮助很大! @Yuvaray,你必须从数据库中取出它,在那里存储它。但请记住,你不知道谁是那个人。你只有一个号码...要个性化你必须有一个用户登录或其他东西。 – webprogrammer 2012-05-03 08:14:15

1

你需要的所有设备令牌存储在您的服务器。你将不得不为此设置一个数据库。要发送推送通知,您需要将您的消息和设备令牌信息从您的服务器发送到APNS。

如果你想推送通知发送到特定的用户,您的应用程序应与设备令牌一起发送用户信息到服务器。您需要将用户信息和设备令牌保存在数据库中。如果您需要向特定用户发送通知,请拿起该用户的设备令牌,形成消息并将其发送给APNS。

您参考两个部分教程有所有这些细节。它有一个可下载的PHP脚本供您试用。

+0

非常感谢您的回答。如果我们的APNS服务器将在.net平台上或者应该在.php文件中,请您澄清一下。你能分享任何示例代码来获取服务器中的所有设备令牌吗?请帮帮我。提前致谢。 – 2012-04-24 15:23:43

+0

您的服务器可以在任何平台上。不必是PHP。问题在于你的服务器应该使用这些指定的方式与APNS(Apple的推送通知服务器)通信。设备令牌通过普通的Web服务从您的应用(IOS设备)发送。您的应用将使用设备令牌和其他信息作为POST数据在服务器上调用Web服务。 – zolio 2012-04-24 23:26:37

+0

你好Zolio谢谢你的回复。你能否澄清这一点?在apple文档中,我们可以使用.pem文件作为保存在本地机器上的服务器。所以,我们如何使用.pem文件作为服务器。我们如何在服务器中编码?提前致谢。 – 2012-04-25 09:39:46

相关问题