发个广告:ios开发两年了,一步步走来 关注公众号一起进步
看到的文章记录
1,环境配置
APNS:Apple Push Notification Service。本文对推送相关概念不再赘述,只侧重完整流程。
Demo 开发环境:Mac os 10.9.4 ,Xcode 6.0.1 ;测试设备:iphone 4s(ios 7.1)
服务端开发环境:mac 10.9.4 + php 5.4.24、
Demo 下载地址:点击打开链接
2,APNS 相关博客
如对apns相关概念不清楚,可参考以下几个博客:(博客中部分内容重复,但总体来说,通读一遍,还是大有裨益的)
http://cshbbrain.iteye.com/blog/1859810 =》IOS 基于APNS消息推送原理与实现(JAVA后台)
http://www.cnblogs.com/qq78292959/archive/2012/07/16/2593651.html =》iOS消息推送机制的实现
http://blog.youkuaiyun.com/xunyn/article/details/8243573 =》APNS编程----iOS真机测试消息推送
http://blog.youkuaiyun.com/wswqiang/article/details/8208581 =》IOS APNS 处理
http://eric-gao.iteye.com/blog/1567777 =》 IOS PEM 文件的生成
http://www.36coder.com/study/996.html =》PHP 实现APNS 推送
http://blog.youkuaiyun.com/sxfcct/article/details/7939082 =》 APNS 相关总结(推荐)
3,APNS 接口
消息推送:
开发接口:gateway.sandbox.push.apple.com:2195
发布接口:gateway.push.apple.com:2195
反馈服务:
开发接口:feedback.sandbox.push.apple.com:2196
发布接口:产品接口:feedback.push.apple.com:2196
二、制作Push证书和Pem文件
1,新建一个App ID
新建流程不再赘述,这里只提醒两点:1》App ID Suffix 中,一定要选择Explicit App ID;2》App Services 中,记得勾选Push Notifications。这里以新建一个id为:com.eversoft.PushDemo 为例。
2,配置push开发证书
在App IDs中,选中刚才新建的App id:com.eversoft.PushDemo ,单击,展开详细信息属性。
在详细信息属性中,单击下方的“Edit”按钮,
在新打开的编辑界面,单击“Create Certificate”,
在新打开的界面中,会提示我们,创建一个csr 证书签名请求文件。具体的创建步骤,界面中已经给出了详细的英文说明。
在进行下一步之前,我们先按照英文说明,创建一个 CSR 文件。
- 在mac电脑上,打开应用程序 keychain(钥匙串访问);
- 在keychain菜单栏中,依次选择“钥匙串访问”=》“证书助理”=》“从证书颁发机构请求证书”;

- 在新打开的“证书助理”界面中,填写用户电子邮件地址,常用名称,CA电子邮件地址,这两个邮件地址直接填写你的苹果账号的邮件地址即可,然后选择“存储到磁盘”,然后点击“继续”;

- 选择CSR文件保存位置,“存储”即可。至此, CSR 文件,制作完成。
回到刚才我们的web页面上,点击“Continue”,进入下一页面;新的页面中,会要求我们上传刚才制作的csr文件,选择“Choose File”,找到我们刚才存储的csr文件,单击“打开”,最后,点击页面上的“Generate”按钮,到此,开发使用的push证书制作完毕。
证书生成成功后,选择“Download”,将制作好的证书下载到本地。然后双击下载的证书aps_development.cer,双击后,证书就自动导入到钥匙串中了。
打开 keychain,左侧钥匙串选择“登录”,种类选择“所有项目”,在右侧窗口中,选中刚才导入的Apple Development IOS Push Services证书(不用选中专用密钥),右键,选择导出,命名为:ck.p12 ,存储时,会提示输入保护密码,这里为演示方便,就输入了123456。之后又会要求输入电脑登录密码,输入即可。
3,生成PEM文件
最后,打开终端,执行以下命令,生成pem文件
openssl pkcs12 -in ck.p12 -out ck.pem -nodes
执行时,会要求输入导入密码,这里输入刚才的保护密码123456即可。

到此,php 服务端使用的pem证书就制作完毕了。
Development PP 文件制作不再赘述。
三、IOS 代码编写
首先,在AppDelegate.m 中:
1,注册通知
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
- ViewController *mainCtrl=[[ViewController alloc] init];
- self.window.rootViewController=mainCtrl;
-
-
- if ([UIDevice currentDevice].systemVersion.doubleValue<8.0) {
- [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
- }
- else {
- [[UIApplication sharedApplication] registerForRemoteNotifications];
- [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil]];
- }
-
-
- if (launchOptions) {
-
- NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
- if (badge>0) {
-
- badge--;
-
- [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
- NSDictionary *pushInfo = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
-
-
- NSString *pushString = [NSString stringWithFormat:@"%@",[pushInfo objectForKey:@"aps"]];
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"finish Loaunch" message:pushString delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];
- [alert show];
- }
- }
-
- return YES;
- }
2,注册通知后,获取device token
- - (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
- NSString *token = [NSString stringWithFormat:@"%@", deviceToken];
- NSLog(@"My token is:%@", token);
-
- }
-
- - (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
- NSString *error_str = [NSString stringWithFormat: @"%@", error];
- NSLog(@"Failed to get token, error:%@", error_str);
- }
3,接收推送通知
- - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
- {
- [UIApplication sharedApplication].applicationIconBadgeNumber=0;
- for (id key in userInfo) {
- NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
- }
-
-
-
-
-
-
-
- UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"remote notification" message:userInfo[@"aps"][@"alert"] delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil nil];
- [alert show];
- }
注意:app 前台运行时,会调用 remote notification;app后台运行时,点击提醒框,会调用remote notification,点击app 图标,不调用remote notification,没反应;app 没有运行时,点击提醒框,finishLaunching 中,launchOptions 传参,点击app 图标,launchOptions 不传参,不调用remote notification。