iOS push notification

Push notification的实现过程
1. 创建SSL证书
首先需要需要创建一个App ID以及关联的SSL证书。有了证书,解析服务器才能找到你的App ID,继而将通知推送到你的应用。
首先创建一个证书请求文件:
a. 在mac上运行钥匙串访问(keychain)
b. 选择钥匙串访问>证书助理>从证书颁发机构中请求一个证书
c. 输入你的名字和邮件地址,CA邮件地址默认就可以,不要试图去进行任何改动。
d. 选择“保存到硬盘”,这样.certSigningRequest的文件生成了。
e. 操作界面如下所示:


然后创建APP ID:
a. 登陆网站Apple Developer Member Center并进入iOS Provisioning Portal。
b. 从左边栏里点击App IDs。
c. 选择New App ID,然后创建一个新的App ID。一定要确保Bundle Identifier一栏中没有星号。
d. 在你的App ID下面找到Configure,选中。
e. 将“Enable for Apple Push Notification service”勾选上,然后点击Development Push SSL Certificate下面的Configure,然后会出现Apple Push Notification service SSL Certificate Assistant设置向导。
f. 点击Continue继续,然后点击Choose File,选中刚创建见的.certSigningRequest文件。
g. 点击Generate开始生成,然后点击Download下载生成的SSL证书。
h. 通过keychain程序来安装下载好的SSL证书。
i. 接着在“我的证书”选项先面,找到你刚才安装名称形如“Apple Development IOS Push Services: xxx”的证书。
j. 双击证书,选择“导出”,导出后的文件后缀名为.p12。这个时候千万注意!出现密码提示的时候一定不要添任何东西。


至此SSL证书创建完成。
2. 创建Provisioning Profile
a. 在 ios provisioning portal中选择provisioning
b. 点击New profile
c. 按照要求填写profile name,certificate,APP ID(刚才创建的app id),devices
d. 下载生成的provisioning profile
e. 双击安装。
   至此前期的准备工作完成,此时你应该有.certSigningRequest  .cer  .p12  .mobileprovision四个文件,现在需要编写代码了。
首先需要编写客户端的代码
a. 需要在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加[application registerForRemoteNotificationTypes:   
     UIRemoteNotificationTypeBadge |  
     UIRemoteNotificationTypeAlert |               
     UIRemoteNotificationTypeSound];  
代码片段。
b.需要继承以下3个方法。
/**接受后台推送的消息*/
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    /**此处要对返回的信息做提示的处理,振动,alert提示*/
}

/**注册成功获取deviceToken*/
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
/**此处需要将生成的deviceToken传到服务端。*/
}

/**注册失败*/
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{

}
服务端这里我们采用的是网上提供的:pushmeBaby。服务端做的事情包括收集deviceToken,推出消息。需要一个APNs的回调service?详见https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html
这个文档。有详细的描述。{"aps":{"alert":"This is some fancy message.","badge":2 ,"sound" : "ping1.caf"}}

猜你喜欢

转载自liuxiangtao.iteye.com/blog/1666913