学习iOS开发已经两年多了,推送方面一直使用第三方极光推送,对推送没有进行系统的学习。今天我就把推送相关的知识点梳理一遍,以供以后的复习与查阅。
iOS 的推送分为本地推送和远程推送。
本地推送
待完善。。。 (偷个懒)
远程推送
1.要先去开发者官网配置推送证书和描述文件
2.在Xcode里点击项目 -> Capabilities 里打开推送功能 (不然获取不到deviceToken)
3.注册通知
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//注册通知 (iOS8 和 iOS 10 推送发生了改变)
if(@available(iOS10.0, *))
{
//iOS10 及以后注册通知
//请求通知权限, 本地和远程共用
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if(granted)
{
NSLog(@"请求成功");
}
else
{
NSLog(@"请求失败");
}
}];
//注册远程通知
[application registerForRemoteNotifications];
//设置通知的代理
center.delegate=self;
}
else if ( [UIDevice currentDevice].systemVersion.floatValue >= 8.0 )
{
//iOS8 及以后注册通知
//1. 配置通知类型
/*
UIUserNotificationTypeBadge = 1 << 0, //角标
UIUserNotificationTypeSound = 1 << 1, //声音
UIUserNotificationTypeAlert = 1 << 2, //提醒
*/
UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
//2. 配置settings
UIUserNotificationSettings * settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];
//3. 注册settings
[application registerUserNotificationSettings:settings];
//4. 注册远程通知
[application registerForRemoteNotifications];
}
else
{
//iOS8 以下注册通知
//1. 配置通知的类型
/*
UIRemoteNotificationTypeBadge = 1 << 0, //角标
UIRemoteNotificationTypeSound = 1 << 1, //声音
UIRemoteNotificationTypeAlert = 1 << 2, //提醒
*/
UIRemoteNotificationType type = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert;
//2. 注册远程通知
[application registerForRemoteNotificationTypes:type];
}
return YES;
}
4.获取deviceToken
//当完成注册远程通知时,接收苹果服务器返回的deviceToken
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSLog(@"deviceToken = %@", deviceToken);
//发送deviceToken给公司服务器 (一般情况下,手机不做删除操作,deviceToken每次运行是不变的)
}
5.收到通知的代理
//当接收到远程推送值的时候会调用 (此方法只有在前台和后台时才会调用。app没启动时,点击推送进来不会调用,需要在didFinishLaunchingWithOptions:里额外处理)
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
/*
UIApplicationStateActive, //激活 (前台)
UIApplicationStateInactive, //将要激活 (将要进入前台)
UIApplicationStateBackground //后台
*/
if ( [UIApplication sharedApplication].applicationState == UIApplicationStateActive )
{
//如果程序在前台运行,那么我们不应该主动发生跳转行为 (也可以给用户提示一下,比如QQ是声音,微信是震动)
NSLog(@"前台收到通知:%@", userInfo);
return;
}
else
{
NSLog(@"后台收到通知:%@", userInfo);
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:view];
}
//程序没启动时收到通知的额外处理
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//收到通知的处理 (iOS7及以下的额外处理)(点击收到的通知进入app后,如果是首次启动,不会执行收到通知的代理,需要在这个方法进行处理)
if ( launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] )
{
NSDictionary * dict = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
label.numberOfLines = 0;
label.backgroundColor = [UIColor yellowColor];
label.text = [NSString stringWithFormat:@"launchOptions = %@", dict];
[self.window.rootViewController.view addSubview:label];
}
return YES;
}
//写了这个方法,上个方法就不会再调用了
//如果实现了这个方法,还要打开一个选项值 -> 点击项目 -> Capabilities -> UIBackgroundModes -> 勾选Remote notifications (如果没打开会打印出警告信息)
//当接收到远程推送值的时候会调用 (此方法在台、后台、退出时都会调用 iOS7新增的方法)
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
/*
UIApplicationStateActive, //激活 (前台)
UIApplicationStateInactive, //将要激活 (将要进入前台)
UIApplicationStateBackground //后台
*/
if ( [UIApplication sharedApplication].applicationState == UIApplicationStateActive )
{
//如果程序在前台运行,那么我们不应该主动发生跳转行为 (也可以给用户提示一下,比如QQ是声音,微信是震动)
NSLog(@"前台收到通知:%@", userInfo);
return;
}
else
{
NSLog(@"后台收到通知:%@", userInfo);
}
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.window.rootViewController.view addSubview:view];
//当实现此方法时,系统就会让我们调用此方法
//需要传人一个值,这个值告诉苹果,当通知发过后,自己有没有处理成功 (默认传入UIBackgroundFetchResultNewData即可)
/*
UIBackgroundFetchResultNewData, //新数据
UIBackgroundFetchResultNoData, //没有数据
UIBackgroundFetchResultFailed //请求失败
*/
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark - UNUserNotificationCenterDelegate ( iOS10 之后)
/*
iOS10之后前台运行收到通知 会调用的方法
(iOS10之前, 在前台运行时, 不会出现通知的横幅)
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
API_AVAILABLE(ios(10.0)){
NSDictionary *userInfo = notification.request.content.userInfo;
NSLog(@"iOS10及以后 前台收到推送: %@", userInfo);
//在前台运行收到通知后, 有哪些效果呈现(声音/提醒/数字角标)
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
/*
iOS10之后 后台运行及程序退出收到通知 会调用的方法
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
API_AVAILABLE(ios(10.0)){
NSDictionary *userInfo = response.notification.request.content.userInfo;
NSLog(@"iOS10及以后 后台收到推送: %@", userInfo);
completionHandler();
}
总结
如果在前台收到通知时,不需要出现通知的横幅和声音,其实写下面这一个代理其实就可以了,这个方法在台、后台、退出都能收到通知。
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}
iOS10之后新出的两个代理,调用了下面的方法后,上面的方法就不会再调用了。(如果没写下面两个方法,iOS10之后的系统也会调用上面的方法。)
// iOS10之后前台运行收到通知 会调用的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
}
//iOS10之后 后台运行及程序退出收到通知 会调用的方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler
{
}