推送分三个阶段:
第一阶段:应用程序的服务器端把要发送的消息、目的iPhone的标识打包,发给APNS。
第二阶段:APNS在自身的已注册Push服务的iPhone列表中,查找有相应标识的iPhone,并把消息发送到iPhone。
第三阶段:iPhone把发来的消息传递给相应的应用程序,并且按照设定弹出Push通知。
具体流程:
1、应用程序注册APNS消息推送。
2、iOS从APNS Server获取devicetoken,应用程序接收device token。
3、应用程序将device token发送给程序的PUSH服务端程序。
4、服务端程序向APNS服务发送消息。
5、APNS服务将消息发送给iPhone应用程序。
正式开始步骤:
一.生成开发环境推送证书和发布环境推送证书
二.利用证书生成p12文件和pem文件
1) 生成p12
2) 生成pem文件
这两个文件是交给服务器用来验证使用
三.程序代码以及设置
1)
在 Background Modes 中将 远程推送开关打开
2)appdelegate中添加推送代码
#pragma mark
注册推送
- ( void )registerPushService
{
#ifdef __IPHONE_8_0
//ios8 注册推送
UIUserNotificationSettings *settings = [ UIUserNotificationSettings settingsForTypes :( UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeSound
| UIRemoteNotificationTypeAlert ) categories : nil ];
[[ UIApplication sharedApplication ] registerUserNotificationSettings :settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
- ( void )registerPushService
{
#ifdef __IPHONE_8_0
//ios8 注册推送
UIUserNotificationSettings *settings = [ UIUserNotificationSettings settingsForTypes :( UIRemoteNotificationTypeBadge
| UIRemoteNotificationTypeSound
| UIRemoteNotificationTypeAlert ) categories : nil ];
[[ UIApplication sharedApplication ] registerUserNotificationSettings :settings];
#else
//register to receive notifications
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#endif
}
iOS8 修改了推送机制,注册方式有一点改动,所以需要分情况注册
- (
void
)application:(
UIApplication
*)application didReceiveRemoteNotification:(
NSDictionary
*)userInfo fetchCompletionHandler:(
void
(^)(
UIBackgroundFetchResult
result))completionHandler
{
{
}
具体的推送过来后的操作在上面这个代理中进行