服务器向客户端推送消息: 当应用程序推到后台,或者根本就没有运行(我们的代码无能为力)
如果这种情况之下,应用程序想和用户交互(传统的做法 不可能) 推送APNS:Apple Push Notification ServicedeviceToken:标示某一台iPhone上的某一个应用程序
1.(用户必须确认需要收到推送消息)注册远程通知中心向APNS获取64位的字符串deviceToken 注册远程通知中心 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
参数: UIRemoteNotificationTypeBadge //应用程序的角标
UIRemoteNotificationTypeSound //推送提示音
UIRemoteNotificationTypeAlert //提示框内容
2.APNS返回deviceToken(进入代理方法) 1)成功获取deviceToken,系统回调 – (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken; 2)获取deviceToken失败,系统回调 – (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error; 3.iPhone将deviceToken上传至服务器(POST) 例: deviceToken: <c389e769 d6ddb7d5 a783a015 ff553d90 5b1e04e2 6fa71ec7 f0aa52ab 4bdcc660> 1.处理字符串,消除空格 和 <> 2.得到位字符串,post至服务器 4.服务器将推送的消息+deviceToken+(SSL & privace key(两个生成一个文件))发送到APNS 5.(验证通过后)APNS将消息发送到iPhone 6.用户点击推送通知,系统回调(在此方法里将角标置零) – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo; 参数: userInfo:包含推送的内容+推送的声音+角标签 1)设置角标 @property(nonatomic) NSInteger applicationIconBadgeNumber; 推送需要证书:该证书是2份 一份给客户端 一份给服务器(客户端的服务器)
服务器和客户端推送证书必须一致