Push Notification, Step by Step Setup.

By | 2013 年 7 月 25 日

坑爹的苹果,居然在提交证书的时候,由于浏览器(自家的Safari)的问题导致提交不成功,换成别的浏览器才成功。
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.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.1.从证书助手获取两个证书,一个在开发中使用,一个在发布的产品中使用(详见1.4中的系统提示)

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.4.点击开发对应的’Config’,然后回出来一个向导告诉你如何生成CSR文件

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.5.在最后一步,选择生成的CSR文件,然后点击‘Generate’。(坑爹的就是Safari下这个按钮点了没有任何效果,换别的浏览器就好了,汗···)

1.6.Download the generated certificate and install on your server.(On mac, just double click the certificate)
1.6.下载生成的证书并且双击它进行安装

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.生成用来从服务器连接APNS的PEM文件

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

2.2.把dev.cer转换成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

2.4.把两个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.
2.5.然后我们可以测试一下是否一切都正常。先运行

telnet gateway.sandbox.push.apple.com 2195

确定可以连接到APNS,然后运行

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

看看pem文件是不是ok的。

3.Setup the application project in xCode.
3.在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.3.在真机上运行应用程序(模拟器是不支持推送的,也无法测试)

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’
3.6.在第一次运行的时候,应该会有系统的alert让你允许推送通知,点’OK’就行。

4.Test the Push Notification For Development
4.然后就可以测试一下推送了。

4.1.paste the following code in to testPush.php file
4.1.把下面这段代码保存为testPush.php文件

 $message,
    '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;
else
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server
fclose($fp);

4.2.Modify the variables at the first few lines according to your situation. The deviceToken will be used here.
4.2.修改文件最开始的几个变量。这里会用到你刚才记录下的deviceToken。

4.3.Running the php code.
4.3.运行一下这段php代码。

php testPush.php

Reference:
http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12
http://article.ityran.com/archives/194
http://www.cocoachina.com/applenews/devnews/2012/1212/5312.html

发表评论

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