2

Am integrating APNS in my iPhone app. I have walked through on Apple Push Notification document provided from Apple. I have a doubt on server side. Please find my doubts below,

  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?

Can you please give some suggestion regarding my questions? I got a sample .php file from this link http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12. Please help me. Thanks in advance.

Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100

2 Answers2

4

You can get it on serverside with this code:

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

Just store your tokens in DB ($_POST['token'])

To send notifications you have to do a loop which looks like this:

//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++;
    }
webprogrammer
  • 792
  • 15
  • 26
  • Hello daniel. Thanks for your great answer. I have one more doubt can you please clarify. 1. How we can pick a specific user's devicetoken and send notifications? 2. Apple thought that .pem file will be the server. some tutorials thought that server will be in php? Can you pelase clarify these? Thanks in advance. – Yuvaraj.M Apr 25 '12 at 09:27
  • If we are using the .pem file store in a local machine that will be a server. So how we can code (your code) in the .pem file. Am not started the process waiting for the certificate from boss. Still am having a lot of doubts on APNS. Can you please help me. Thanks. – Yuvaraj.M Apr 25 '12 at 09:36
  • I'm trying the same thing atm. I want to send personalized push notifications. My thought is, that I will store the device token into local storage and get them via javascript in my PhoneGap app. Here is a great tutorial for the .pem file: http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 Just follow it step-by-step. It helped me a lot! @Yuvaray, you have to get it out of your database, where you store it. But keep in mind that you don't know who's that guy. You just have a number... To personalize you must have a userlogin or something else. – webprogrammer May 03 '12 at 08:14
1

You need to store all device tokens in your server. You will have to set up a database for this. To send the push notifications, you will send your message and device tokens information to the APNS, from your server.

If you want to send push notification to a specific user, your app should send the user information to your server along with the device tokens. And you need to keep the user information and device tokens in your database. If you need to send a notification to a particular user, pick up that user's device token, form the message, and send it to the APNS.

The Two part tutorial you referenced has all these details. It has a downloadable script in php for you to try out.

zolio
  • 2,439
  • 4
  • 22
  • 34
  • Thanks a lot for your answer. Can you please clarify this one, if our APNS server will be in .net platform or should be in .php file. Can you please share any sample code to get all device tokens in the server? Please help me. Thanks in advance. – Yuvaraj.M Apr 24 '12 at 15:23
  • Your server can be in any platform. Need not be php. The point is that your server should communicate to the APNS (Apple's Push Notification Server) with those specified manner. Device tokens are sent from your app (the IOS device) through normal web services. Your app will call a web service on your server with device token and other information as POST data. – zolio Apr 24 '12 at 23:26
  • Hello Zolio thanks for your reply. Can you please clarify this. In apple document we can use the .pem file as a server which is saved in local machine. So, how we can use .pem file as a server. how we can code in the server? Thanks in advance. – Yuvaraj.M Apr 25 '12 at 09:39
  • .pem file is not used as a server. It is a file format that server can create. To understand what goes inside the server code, I would suggest to look at the server code provided in that example. That is a php script. If possible go through those steps one by one and make your first Push Notification work. It will give you good insight what needs to be achieved in your server code. – zolio Apr 25 '12 at 15:00
  • Hello Zolio Thanks for your great guide. I like your approach whenever i asking you doubt you are responding me. Thanks a lot. I understand that i need to create a server for APNS the server may be in any platform. Am i right. I ve spent more time to find a sample code to save the Apple Push notification device token in our server and retrieve all device token/a specific user's device token. Can you please suggest any sample code or tutorial for me. Thanks in advance. – Yuvaraj.M Apr 26 '12 at 04:47