1、         用钥匙串新建一个证书,在建推送证书的时候选择该证书

2、         新建AppId,注意的是得使Push notification设置为endabled。点击“Genetrate”这时会下载一个aps_development.cer,点击安装,

打开钥匙串,将推送证书和秘钥导出p12文件

3、         新建配置文件PushProfile,在此选择新建的AppId,然后点击“setting”,下载一个证书,点击download,并安装。

4、         在终端将这两个p12文件转换为pem文件,然后把这两个pem文件合并为一个pem文件,合并后的pem文件在此例中为ck.pem,将ck.pem和deviceToken发送给服务器,就可以测试远程推送了

5、         注意的是在终端运行的命令:例如:

cat /Users/huan/Desktop/cert.pem /Users/huan/Desktop/key.pem >/Users/huan/Desktop/ck.pem,要加上应用程序路径。

6、         注意:要修改应用程序的bundle indetifier,和AppId相同。程序中要选择此次新建的配置文件

合并pem文件步骤如下:

1.将推送证书导出的p12文件cert.p12转换为pem文件

openssl pkcs12 -clcerts -nokeys -out cert.pem -in cert.p12

2、将秘钥导出的p12文件key.p12转换为pem文件

openssl pkcs12 -nocerts -out key.pem -in key.p12

3、将新生成的两个pem文件合并为一个pem文件:ck.pem

cat cert.pem key.unencrypted.pem > ck.pem

最后将该ck.pem发送个后台就可以了

接下来就是代码中的操作了,代码比较简单;

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        
  2.     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];  
  3.         // other codes here.    
  4.     return YES;
  5. }
  6. - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  7.     NSLog(@"deviceToken: %@", deviceToken);
  8. }
  9. - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  10.     NSLog(@"Error in registration. Error: %@", error);
  11. }
  12. - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
  13. {
  14.     
  15.     NSLog(@"收到推送消息 : %@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]);
  16.     if ([[userInfo objectForKey:@"aps"] objectForKey:@"alert"]!=NULL) {
  17.         UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"推送通知"
  18.                                                         message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]
  19.                                                        delegate:self
  20.                                               cancelButtonTitle:@" 关闭"
  21.                                               otherButtonTitles:@" 更新状态",nil];
  22.         [alert show];
  23.         [alert release];
  24.     }
  25. }

至此,就是完整的推送操作了。

 

posted on 2013-10-23 10:04  瓜甜甜  阅读(442)  评论(0编辑  收藏  举报