ios10推出了全新的UserNotification框架(ios10之前属于UIKit框架)。
远程通知的演练
1,在AppDelegate中,遵循代理UNUserNotificationCenterDelegate,导入头文件:
#import <UserNotifications/UserNotifications.h>
2,在application:didFinishLaunchingWithOptions方法中, 注册远程通知
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//请求通知权限, 本地和远程共用
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"请求成功");
} else {
NSLog(@"请求失败");
}
}];
//注册远程通知
[[UIApplication sharedApplication] registerForRemoteNotifications];
//设置通知的代理
center.delegate = self;
return YES;
}
3,在接收远程推送的DeviceToken方法中, 获取Token
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
//将来需要将此Token上传给后台服务器
NSLog(@"token:%@", deviceToken);
}
ios10远程通知的处理方法
以及实现下面实现3个方法, 用于处理点击通知时的不同情况的处理(这3个方法都是UNUserNotificationCenterDelegate协议的方法)
- willPresentNotification:withCompletionHandler 用于前台运行
- didReceiveNotificationResponse:withCompletionHandler 用于后台及程序退出
- didReceiveRemoteNotification:fetchCompletionHandler: 用于静默推送
/**
前台运行 会调用的方法
iOS10之前, 在前台运行时, 不会出现通知的横幅
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
NSDictionary *userInfo = notification.request.content.userInfo;
//前台运行推送 显示红色Label
[self showLabelWithUserInfo:userInfo color:[UIColor redColor]];
//可以设置当收到通知后, 有哪些效果呈现(声音/提醒/数字角标)
completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
}
/**
后台运行及程序退出 会调用的方法
*/
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler
{
NSDictionary *userInfo = response.notification.request.content.userInfo;
//后台及退出推送 显示绿色Label
[self showLabelWithUserInfo:userInfo color:[UIColor greenColor]];
completionHandler();
}
/**
iOS10中, 此方法主要处理静默推送 --> iOS7以后出现, 不会出现提醒及声音
1. 推送的payload中不能包含alert及sound字段
2. 需要添加content-available字段, 并设置值为1
例如: {"aps":{"content-available":"1"},"PageKey":"2"}
*/
//如果是以前的旧框架, 此方法 前台/后台/退出/静默推送都可以处理
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//静默推送 显示蓝色Label
[self showLabelWithUserInfo:userInfo color:[UIColor blueColor]];
completionHandler(UIBackgroundFetchResultNewData);
}
//将通知的值显示在主界面上
- (void)showLabelWithUserInfo:(NSDictionary *)userInfo color:(UIColor *)color
{
UILabel *label = [UILabel new];
label.backgroundColor = color;
label.frame = CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 300);
label.text = userInfo.description;
label.numberOfLines = 0;
[[UIApplication sharedApplication].keyWindow addSubview:label];
}
网友评论