2012-04-24 5 views
2

私のiPhoneアプリにAPNSを統合しています。私はAppleから提供されたApple Push Notificationの文書を歩きました。私はサーバー側で疑問があります。下記の疑問を見つけてください。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? 

私の質問に関していくつかご提案いただけますか?このリンクからサンプル.phpファイルを入手しましたhttp://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12。私を助けてください。前もって感謝します。

答えて

4

あなたは、このコードをサーバー側でそれを得ることができます次のようになります。

//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つの疑いがあります。 1.特定のユーザーのdevicetokenを選択して通知を送信する方法は? 2. Appleは.pemファイルがサーバーになると考えました。いくつかのチュートリアルは、サーバーがPHPになると思った?これらを明確にすることはできますか?前もって感謝します。 –

+0

サーバーとなるローカルマシンに.pemファイルストアを使用している場合。だから、どのようにして.pemファイルにコード(あなたのコード)を書くことができますか?上司からの証明書を待っているプロセスを開始していない。まだAPNSに疑問を抱いています。手伝ってくれませんか。ありがとう。 –

+0

私は同じことをatmしようとしています。パーソナライズされたプッシュ通知を送信したい私の考えは、デバイストークンをローカルストレージに保存し、PhoneGapアプリケーションのjavascript経由で取得するということです。ここ は.PEMファイルのための素晴らしいチュートリアルです: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 ちょうどそれをステップ・バイ・ステップに従います。それは私をたくさん助けました! @Yuvaray、あなたはそれをあなたのデータベースから取り出して保存しなければなりません。しかし、あなたはその人が誰なのか分からないことに留意してください。あなたは数字だけを持っています...あなたをパーソナライズするには、ユーザーログインなどが必要です。 – webprogrammer

1

すべてのデバイストークンをサーバーに保存する必要があります。このためにデータベースを設定する必要があります。プッシュ通知を送信するには、メッセージとデバイストークン情報をサーバーからAPNSに送信します。

プッシュ通知を特定のユーザーに送信する場合は、デバイストークンとともにサーバーにユーザー情報を送信する必要があります。また、ユーザー情報とデバイストークンをデータベースに保存する必要があります。特定のユーザーに通知を送信する必要がある場合は、そのユーザーのデバイストークンを取得し、メッセージを作成してAPNSに送信します。

参照した2つの部分のチュートリアルには、これらすべての詳細があります。それはあなたが試してみるために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]; 
} 

ジャスト($ _POST [「トークン」])DBにあなたのトークンを保存

+0

お返事ありがとうございます。 APNSサーバーが.netプラットフォームにあるか、.phpファイルである必要がある場合は、このファイルを明確にしてください。サーバーのすべてのデバイストークンを取得するためのサンプルコードを共有できますか?私を助けてください。前もって感謝します。 –

+0

サーバーはどのプラットフォームにもインストールできます。 PHPである必要はありません。要点は、あなたのサーバーがAPNS(Appleのプッシュ通知サーバー)と指定された方法で通信する必要があることです。デバイストークンは、通常のWebサービスを通じてアプリ(IOSデバイス)から送信されます。あなたのアプリはサーバー上でデバイストークンとその他の情報をPOSTデータとしてWebサービスを呼び出します。 – zolio

+0

こんにちはゾリオはあなたの返事に感謝します。あなたはこれを明確にしていただけますか?リンゴ文書では、.pemファイルをローカルマシンに保存されたサーバーとして使用できます。では、.pemファイルをサーバーとして使用する方法について説明します。サーバーでどのようにコード化できますか?前もって感謝します。 –

関連する問題