您的位置:首页 > 移动开发 > IOS开发

iOS push notification

2012-04-23 15:13 309 查看
先记录一些好的blog 回头再整理

1、http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 a good toturial

2、https://www.parse.com/tutorials/ios-push-notifications a simple sample

3、http://eddyl.com/blog/2010/09/push_notification/ use urban airship

4、https://code.google.com/p/pubsubhubbub/ pubsubhubbub

5、http://urbanairship.com/ urbanairship

6、http://quickblox.com 很好很强大

7、http://code54.com/blog/2012/04/13/pushing-messages-ios-apns.html another blog

8、http://blog.csdn.net/sjzsp/article/details/6323546

步骤:

1、用keychain生成PushNotification.certSigningRequest

2、用keychain把私钥PushNotification.certSigningRequest导成PushMsg_Key.p12

3、登录developer.apple.com 为要接收push notification的app新建appID,把这个appID的Development Push SSL Certificate打开,要用到第1步生成的CSR。设置成功,会为这个appID生成aps_development.cer,下载到本地。

4、为这个app新建provisioning profile,设置时AppID要设置成第3步骤新建的AppID,新建后下载到本地,用xCode打开就会加入到xCode里去,要用这个profile把app写进你调试的device。注:push notification需要要真机上才能看到起作用,在模拟器上不可以的。

5、把aps_development.cer 转成apns_dev_cert.pem openssl x509 -in aps_development.cer -inform DER -out apns_dev_cert.pem -outform PEM

6、把PushMsg_Key.p12 转成apns_dev_key.pem openssl pkcs12 -nocerts -out apns_dev_key.pem -in PushMsg_Key.p12

7、把apns_dev_cert.pem与apns_dev_key.pem 合并成apns_dev.pem cat apns_dev_cert.pem apns_dev_key.pem > apns_dev.pem

8、至此万事具备,只欠东风,测试下与APNs的连接

8.1 telnet gateway.sandbox.push.apple.com 2195

连接成功会看到类似:

Trying 17.172.232.226...
Connected to gateway.sandbox.push-apple.com.akadns.net.
Escape character is '^]'.


ctrl+c 退出连接,用上面步骤生成的pem文件测试连接:

8.2 openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert apns_dev_cert.pem -key apns_dev_key.pem

9、开始自己的App,xCode新建,注意AppID要设置成与第3步的一样

9.1 在appDelegate.m 文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//self.window.rootViewController = self.viewController;
[self.windowmakeKeyAndVisible];
// Let the device know we want to receive push notifications
[[UIApplicationsharedApplication]registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert)];
return YES;
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
NSLog(@"Failed to get token, error: %@", error);
}

9.2 执行此app,把deviceToken记录下来。记得要用真机调试

10、准备server,把要发送的msg发送到APNs上面。这个server就有很多种实现方法了,可以在本地用php脚本,可以用quickblox、urban Airship等第三方服务,本地php脚本最方便,把9.2拿到deviceToken与apns_dev_key.pem 的密码填写到脚本里去,执行此脚本,就可以把message push 到APNs, APNs再把message push到你手机。php脚本可参考附件。

以上web 端是用php写的,简要来说需要生成本地的一个key: apns_dev_key.pem,需要从apple developer生成一个key: apns_dev_cert.pem,再把这两个key合成:apns_dev.pem 给php用。

注:以上是开发阶段的设置,等真正发布appstore或app AdHoc时,设置有些细微差别,不然导致发布的版本push message failed掉。
AppStore 与 App AdHoc两种发布方式的设置:
1、以上第1、2步生成的PushNotification.certSigningRequest与apns_dev_key.pem继续使用。
2、登录Provisioning Portal 把 AppID 的 Production Push SSL Certificate启用,用到PushNotification.certSigningRequest。启用后生成:aps_production.cer,下载本地
3、与开发时的设置一样,把aps_production.cert转成apns_production_cert.pem,然后将apns_production_cert.pem与apns_dev_key.pem结合生成apns_production.pem
4、PHP发送脚本需要修改
4.1 APNS服务器地址改为:ssl://gateway.push.apple.com:2195',开发时是:ssl://gateway.sandbox.push.apple.com:2195'
4.2 发送的证书要使用apns_production.pem。
4.3 最关键一步,同一个iphone,开发与发布两种状态的token是不一样的,所以这里需要修改token。(用发布的程序抓取)

如果web端用java写,要如下设置:

java代码比较简单,可是使用开源的类库 如https://github.com/notnoop/java-apns

估计卡住的人通常是和apns握手失败。这原因主要是java、.net和mac的ssl连接区别.

java需要把下载的文件再次转换才可以使用。

apns_dev_key.pem 与 apns_dev_cert.pem 用以上步骤生成的,最后用以下命令合成apns_dev_key_for_java.p12,给java用

openssl pkcs12 -export -in apns_dev_cert.pem -inkey apns_dev_key.pem -certfile PushNotification.certSigningRequest -name "apns_dev_key_for_java" -out apns_dev_key_for_java.p12

完毕
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: