Push Notification, Step by Step Setup.

By | 2013 年 7 月 25 日

The device token for Development and for Production is DIFFERENT!

(!!! DO NOT use the Safari, When access the develop.apple.com)
1.Access the certificate for push notification

1.1.Request 2 certificate from the KeyChain Assistant (CSR), one for develop, one for production. If you do not know how to do it, see Instruction 1.4 below. And export the associated Private Key as .p12 files. So after this step, we get 4 files.

1.2.On developer.apple.com in the Provisioning Portal, Select the App Id that you want to enable Push Notification, and click the ‘Config’ button.
1.2.在developer.apple.com的Provisioning Portal中,选择要发送推送通知的App Id,点击“Config”按钮

1.3.Check the Enable Push Notification Check box
1.3.勾上Enable Push Notification多选框

1.4.Click the ‘Config’ button for develop, and there is a guide for ‘How to request a certificate from KeyChain Assistant’. When you’ve done, click the ‘Continue’.

1.5.Select the CSR file that generated by last step, then click ‘Generate’. (In this step, DO NOT use the Safari, or the ‘generate’ button will not response to your click at all……)

1.6.Download the generated certificate and install on your server.(On mac, just double click the certificate)

1.7.Do the same for Production, following step 1.4,1.5,1.6.
1.7.对于发布版本,按照1.4, 1.5 和1.6的步骤再来一次。

2.Build the .pem file for our server to connect to APNS.

2.1.Copy the .p12, .csr, .cer files together in one place.
2.1.将前面得到的.p12, .csr, .cer文件放在一起。这时一共有6个文件,对应Develop,比如叫做dev.p12,dev.csr,dev.cer;对于发布有另外三个文件,这里以dev为例

2.2.Convert the dev.cer into devCer.pem. In the terminal, run

openssl x509 -in dev.cer -inform der -out devCer.pem


openssl x509 -in dev.cer -inform der -out devCer.pem

2.3.Conver the dev.p12 into devKey.pem. In the terminal, run openssl pkcs12 -nocerts -out devKey.pem -in dev.p12. First you will be asked to input the password for the dev.p12 file, and then will let you input your password for devKey.pem file.
2.3.把dev.p12转换成devKey.pem. 在终端中运行 openssl pkcs12 -nocerts -out devKey.pem -in dev.p12。中间会让你首先输入.p12文件的密码,然后让你输入密码来保护生成的.pem文件

2.4.Combine the two pem file together. In the terminal, run

cat devCer.pem devKey.pem > dev.pem


cat devCer.pem devKey.pem > dev.pem

2.5.Then we can test if things goes well. By run

telnet gateway.sandbox.push.apple.com 2195

to see if you can reach the APNS. Then run

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert devCer.pem -key devKey.pem

to see if the two pem files are ok.

telnet gateway.sandbox.push.apple.com 2195


openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert devCer.pem -key devKey.pem


3.Setup the application project in xCode.

3.1.Open the application project in xCode, and code sign with the specific (the exact same bundle id) provisioning file. If you want debug the push notification in xCode, be sure the right develop provisioning file is used.
3.1.在code sign部分,选择正确的provisioning file

3.2.Add some code to enable push notification in your application as:
3.2.在app delegate中添加以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    //Some other code here ... for the window or something.
    return YES;
#pragma mark notification
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"Failed to Get Token, Error: %@", error);
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    NSLog(@"Success Get Token : %@", deviceToken);

3.3.Run your application in the real device, not simulator(the simulator is not support the push notification)

3.4.If you get the log of “Failed to Get Token”, then you must have something wrong in the previous steps.
3.4.如果发现输出了”Failed to Get Token”,说明前面步骤上有问题。

3.5.Else you will get a log like : “Success Get Token : <90757852 {6 segment of this kind of hex numbers} 82728ad7>“, record this deviceToken, which will be used for testing.
3.5.正常情况下,你可以获得一条消息”Success Get Token : <90757852 {6 segment of this kind of hex numbers} 82728ad7>“,记录下这段deviceToken,后面的测试会用到它

3.6.On your device, there will be an alert to ask permission for push notification, tap on ‘OK’

4.Test the Push Notification For Development

4.1.paste the following code in to testPush.php file

    'sound' => 'default'

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

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

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

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

// Close the connection to the server

4.2.Modify the variables at the first few lines according to your situation. The deviceToken will be used here.

4.3.Running the php code.

php testPush.php



电子邮件地址不会被公开。 必填项已用*标注