iOS的推送机制

来源:互联网 发布:热传导 软件 编辑:程序博客网 时间:2024/05/15 16:22

iOS推送机制的原理:

1、应用程序注册消息推送;

2、iOS从APNS Server获取device token;

3、应用程序将device token发送给PUSH服务器程序;

4、服务端程序向APNS服务发送消息;

5、APNS服务将信息发送给iPhone应用程序;

注:推送需要通过证书进行链接


获取证书的步骤:

一、首先需要一个CSR文件,创建CSR文件的方法:

1、首先打开mac中的钥匙串访问,选择钥匙助理--》从证书颁发机构请求证书;

2、填写邮箱和mac常用名称,并选择保存到磁盘;

这样就在本地生成了一个CSR文件。


二、生成p12文件

1、在钥匙串访问中找到刚才生成的CSR文件;

2、右键选择导出;

3、在导出过程中会提示输入密码,自己设置一个即可;

这就生成了一个p12文件。


三、使用付费账号登录:https://developer.apple.com

1、进入iOS  Provisioning Portal,并创建一个新的App ID;

2、创建好App ID后点击对应的右边的“configure”;

3、然后点击Development  Push  SSL  Certificate一行最后的"configure";

4、点击Continue;

5、点击选择文件将上面使用钥匙串访问的CSR文件上传,然后点击Generate;

6、点击Continue;

7、此时会生成一个证书,点击Download将此证书下载到本地,然后点击Done;


此时我们已经有了CSR、p12、证书这三个文件了,接下里就是在项目中通过代码进行操作

四、工程配置:

1、在AppDelegate.m文件中的didFinishLaunchingWithOptions方法中加入以下代码:

/// @brief 注册推送

[[UIApplication  sharedApplication]  registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];


2、在AppDelegate.m文件中添加一下两个方法来获取device Token:

- (void)application:(UIApplication *)app  didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{

NSString *token = [NSString stringWithFormat:@"%@",deviceToken];

NSLog(@"my token is %@",token);

/// @brief 将device token发送到自己的服务器

DeviceSender  *sender = [[[DeviceSender alloc] initWithDelegate:self] autorrlease];

}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

NSString  *error_token = [NSString  stringWithFormat:@"%@",error];

NSLog(@"fail to get token,error is :%@",error_token);

}


获取到device Token后将其提交给服务器,但发送通知的服务器除了知道device Token之外,还需要一个连接APNS的证书,此证书可以通过我们前面生成的两个证书得到:

1、将aps_developer_identity.cer转换成 aps_developer_identity.pem格式。

  openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM

  2、将p12格式的私钥转换成pem,需要设置4次密码,密码都设置为:abc123。

  openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChat.p12

  3、用certificate和the key 创建PKCS#12格式的文件。

  openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name aps_developer_identity -out aps_developer_identity.p12

  这样我们就得到了在.net应用程序中使用的证书文件:aps_developer_identity.p12。



0 0
原创粉丝点击