公司新增推送服务器,要用源生,一两年前写过,不过最近几个项目是用极光,现在再整理一下,废屁不多放,看
源生推送和极光的区别:
1. 注册推送 这点一样,是极光推送进行加层封装了一下
2.获取token ,注意token开发和发布获取的不同,不能通用
3.服务器逻辑:
源生:自己服务器拿证书,token,消息json串(角标,声音等)去请求APNs苹果推送服务器
极光推送:自己服务器先给极光发送需要推送的一些设置,请求APNs 由极光去做
4.实现代理:
源生:实现系统代理,处理推送结果,以及一些其他逻辑
极光推送:实现极光的代理(就是极光对系统代理进行了封装),推送结果先走到系统方法中,极光取出来处理到自己的代理方法中,所以我们用极光代理就如同用系统代理,没差。(SDK实现想想就是简简单单)
下面说一下源生(极光的自己去官网看文档):
- 注册推送逻辑
/** 注册推送逻辑 ,代码内部分iOS 10 以上 10-8 ,8 以下(项目适配到8,然后就注释掉了) */ -(void)registerNoticaiton{ if (@available(iOS 10 , *)) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center setDelegate:(AppDelegate*)self]; [center requestAuthorizationWithOptions:UNAuthorizationOptionSound|UNAuthorizationOptionAlert|UNAuthorizationOptionBadge completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { //注册成功 NSLog(@"注册成功"); } else{ //注册失败 NSLog(@"注册失败"); } }]; } else if (@available(iOS 8.0,*)){ UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge categories:nil]; [[UIApplication sharedApplication]registerUserNotificationSettings:setting]; } // else{//ios 8 以下 // [[UIApplication sharedApplication]registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound]; // // } //注册获得 device token [[UIApplication sharedApplication]registerForRemoteNotifications]; }
- 获取device token,需要把符号去掉,取出一串字符串
#pragma mark - 获取device token -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ //获取devicetoken ,这里发送给后台,后台拿token ,证书 和 推送消息去请求 APNs服务器 NSString *deviceTokenStr = [[[[deviceToken description] stringByReplacingOccurrencesOfString:@"<" withString:@""] stringByReplacingOccurrencesOfString:@">" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"deviceTokenStr:\n%@",deviceTokenStr); } -(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ //获取device token 失败报错 NSLog(@"获取deviceTokenError:%@",error); }
- 实现代理方法,需要引入头文件,遵循代理,代理对象为APPdelegate,在注册推送逻辑那块写过了,iOS10以下不用遵守代理,直接系统回调方法。
#pragma mark - 处理接收到的推送消息 ios 10.0 以上
/*
处理是否通知用户,前台状态也能收到
*/
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
{
NSDictionary *info = notification.request.content.userInfo;
NSLog(@"--------%@",info);
// 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
if([UIApplication sharedApplication].applicationState == UIApplicationStateActive){//前台,当在前台的时候,推送走这里
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送消息" message:info[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
completionHandler(UNNotificationPresentationOptionBadge);
}else{//后台和杀死状态
completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert|UNNotificationPresentationOptionSound);
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
//handle code here...
NSLog(@"回到前台");//这里还要做详细的处理逻辑, 当从桌面点击打开应用的时候,走这里
if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
[UIApplication sharedApplication].applicationIconBadgeNumber -= 1 ;
}
}
/*
客户端收到消息,处理跳转等 ,当从通知栏点击进入的时候
*/
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0)
{
NSDictionary *info = response.notification.request.content.userInfo;
NSLog(@"info:%@",info[@"aps"]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:info[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
// if([UIApplication sharedApplication].applicationIconBadgeNumber>0){
// [UIApplication sharedApplication].applicationIconBadgeNumber -= 1 ;
// }
completionHandler();
}
#pragma mark - 处理接收到的推送消息 ios 10.0 以下
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"didReceiveRemoteNotification:%@",userInfo);
//处理应用在前台,后台,和死亡状态下
if (application.applicationState == UIApplicationStateActive) {//用户前台
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送消息" message:userInfo[@"aps"][@"alert"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
[alert show];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
}else{//用户后台和杀死状态下
//点击推送消息,处理时间
}
completionHandler(UIBackgroundFetchResultNewData);
}
PS:实在不想写啥了,代码里面是自己的测试简单逻辑,没在自己公司服务器上测,公司后台忙着开发接口,用的网上别人写的测试工具,就是MAC上的一个小程序,自己写也可以(问题是要有服务器)。还有好多配置别忘了,推送开关 ,后台状态等等。敢敢单单