1、注册
#pragma mark - 注册远程消息通知
-(void)registerAPNs:(UIApplication *)application
{
/*
__IPHONE_8_0 :代表编译器的SDK版本在IOS8以上
[[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 :
是判断运行时的SDK版本在IOS8以上
*/
#ifdef __IPHONE_8_0
//在IOS8下
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:uns];
}
else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
}
#else
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
#endif
}
2、获取deviceToken 3、将deviceToken发送给后台服务器
#pragma mark - 回调方法 成功注册远程消息通知
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
NSString *deviceToken = [NSString stringWithFormat:@"%@", pToken];
NSLog(@"deviceToken:%@", deviceToken);
// deviceToken:<15f18a2b ea039e1a e51584f9 09597d79 4337c61a b74524cc b74a12a0 c10a972a>
//发送后台服务器之前,要把空格去掉,<>去掉
//将deviceToken发送到自己的后台服务器
if (deviceToken) {
[self postDeviceToken:deviceToken];
}
}
#pragma mark - 回调方法 注册远程消息通知失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Register fail%@",error);
}
#pragma mark - 将deviceToken发送到自己的后台服务器
-(void)postDeviceToken:(NSString *)deviceToken
{
NSLog(@"在这里 把deviceToken 发送给后台服务器, 让后台服务器保存deviceToken");
}
4、后台服务器向APNs服务器发送推送消息 5、接收推送消息
#pragma mark - 回调方法 成功接收推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//处理推送消息
NSLog(@"userInfo == %@", userInfo);
/*
推送过来接收到得信息,其中aps中,alert,badge,sound是苹果固定的格式,不能修改,但是苹果推送消息,不能超过256字节也就是128个汉字,输入超过,推送失败
{
aps = {
alert = "hello";
badge = 1;
sound = default;
};
}
*/
//处理推送过来的消息
[self DealNotification:userInfo];
}
#pragma mark - 处理推送过来的消息
-(void)DealNotification:(NSDictionary *)userInfo
{
NSLog(@"在这里 处理推送过来的数据");
}
#pragma mark - 注册远程消息通知
-(void)registerAPNs:(UIApplication *)application
{
/*
__IPHONE_8_0 :代表编译器的SDK版本在IOS8以上
[[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 :
是判断运行时的SDK版本在IOS8以上
*/
#ifdef __IPHONE_8_0
//在IOS8下
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:nil];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:uns];
}
else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
}
#else
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
#endif
}
2、获取deviceToken 3、将deviceToken发送给后台服务器
#pragma mark - 回调方法 成功注册远程消息通知
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)pToken {
NSString *deviceToken = [NSString stringWithFormat:@"%@", pToken];
NSLog(@"deviceToken:%@", deviceToken);
// deviceToken:<15f18a2b ea039e1a e51584f9 09597d79 4337c61a b74524cc b74a12a0 c10a972a>
//发送后台服务器之前,要把空格去掉,<>去掉
//将deviceToken发送到自己的后台服务器
if (deviceToken) {
[self postDeviceToken:deviceToken];
}
}
#pragma mark - 回调方法 注册远程消息通知失败
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
NSLog(@"Register fail%@",error);
}
#pragma mark - 将deviceToken发送到自己的后台服务器
-(void)postDeviceToken:(NSString *)deviceToken
{
NSLog(@"在这里 把deviceToken 发送给后台服务器, 让后台服务器保存deviceToken");
}
4、后台服务器向APNs服务器发送推送消息 5、接收推送消息
#pragma mark - 回调方法 成功接收推送消息
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//处理推送消息
NSLog(@"userInfo == %@", userInfo);
/*
推送过来接收到得信息,其中aps中,alert,badge,sound是苹果固定的格式,不能修改,但是苹果推送消息,不能超过256字节也就是128个汉字,输入超过,推送失败
{
aps = {
alert = "hello";
badge = 1;
sound = default;
};
}
*/
//处理推送过来的消息
[self DealNotification:userInfo];
}
#pragma mark - 处理推送过来的消息
-(void)DealNotification:(NSDictionary *)userInfo
{
NSLog(@"在这里 处理推送过来的数据");
}