2012-04-24 54 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

相關問題