iOS10系统最大的一个亮点就是增加了系统应用的拓展功能Extension
iOS10之后,为了对自定义通知界面拓展Notification Content
的支持,iOS系统推出了新的框架
1.请求授权及添加分类
#import "AppDelegate.h"//iOS10通知新框架
#import
//iOS10 自定义通知界面
#import
}//当APP处于前台的时候接收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{//弹出一个网页UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 400, 500)];webview.center = self.window.center;[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itheima.com"]]];[self.window addSubview:webview];//弹出动画webview.alpha = 0;[UIView animateWithDuration:1 animations:^{webview.alpha = 1;}];}#pragma mark - 创建通知分类(交互按钮)- (UNNotificationCategory *)createCatrgory
{//文本交互(iOS10之后支持对通知的文本交互)/**optionsUNNotificationActionOptionAuthenticationRequired 用于文本UNNotificationActionOptionForeground 前台模式,进入APPUNNotificationActionOptionDestructive 销毁模式,不进入APP*/UNTextInputNotificationAction *textInputAction = [UNTextInputNotificationAction actionWithIdentifier:@"textInputAction" title:@"请输入信息" options:UNNotificationActionOptionAuthenticationRequired textInputButtonTitle:@"输入" textInputPlaceholder:@"还有多少话要说……"];//打开应用按钮UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"foreGround" title:@"打开" options:UNNotificationActionOptionForeground];//不打开应用按钮UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"backGround" title:@"关闭" options:UNNotificationActionOptionDestructive];//创建分类/**Identifier:分类的标识符,通知可以添加不同类型的分类交互按钮actions:交互按钮intentIdentifiers:分类内部标识符 没什么用 一般为空就行options:通知的参数 UNNotificationCategoryOptionCustomDismissAction:自定义交互按钮 UNNotificationCategoryOptionAllowInCarPlay:车载交互*/UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[textInputAction,action1,action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];return category;
}//按钮点击事件
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{//根据identifer判断按钮类型,如果是textInput则获取输入的文字if ([response.actionIdentifier isEqualToString:@"textInputAction"]) {//获取文本响应UNTextInputNotificationResponse *textResponse = (UNTextInputNotificationResponse *)response;NSLog(@"输入的内容为:%@",textResponse.userText);}//处理其他时间NSLog(@"%@",response.actionIdentifier);
}- (void)applicationWillResignActive:(UIApplication *)application {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}- (void)applicationDidEnterBackground:(UIApplication *)application {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}- (void)applicationWillEnterForeground:(UIApplication *)application {// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}- (void)applicationDidBecomeActive:(UIApplication *)application {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}- (void)applicationWillTerminate:(UIApplication *)application {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}@end
#pragma mark - 发送本地通知- (IBAction)sendLocalNotification:(id)sender {//1.创建通知中心UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];//2.检查当前用户授权[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {NSLog(@"当前授权状态:%zd",[settings authorizationStatus]);//3.创建通知UNMutableNotificationContent *notification = [[UNMutableNotificationContent alloc] init];//3.1通知标题notification.title = [NSString localizedUserNotificationStringForKey:@"传智播客" arguments:nil];//3.2小标题notification.subtitle = @"hellow world";//3.3通知内容notification.body = @"欢迎来到黑马程序员";//3.4通知声音notification.sound = [UNNotificationSound defaultSound];//3.5通知小圆圈数量notification.badge = @2;//4.创建触发器(相当于iOS9中通知触发的时间)/**通知触发器主要有三种UNTimeIntervalNotificationTrigger 指定时间触发UNCalendarNotificationTrigger 指定日历时间触发UNLocationNotificationTrigger 指定区域触发*/UNTimeIntervalNotificationTrigger * timeTrigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];//5.指定通知的分类 (1)identifer表示创建分类时的唯一标识符 (2)该代码一定要在创建通知请求之前设置,否则无效notification.categoryIdentifier = @"category";//给通知添加附件(图片 音乐 电影都可以)NSString *path = [[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"];UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"image" URL:[NSURL fileURLWithPath:path] options:nil error:nil];notification.attachments = @[attachment];//7.创建通知请求/**Identifier:通知请求标识符,用于删除或者查找通知content:通知的内容trigger:通知触发器*/UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"localNotification" content:notification trigger:timeTrigger];//8.通知中心发送通知请求[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {if (error == nil) {NSLog(@"通知发送成功");}else{NSLog(@"%@",error);}}];}];
}
#pragma mark - 移除所有通知
- (IBAction)removeAllNotification:(id)sender {//1.创建通知中心UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];//2.删除已经推送过得通知[center removeAllDeliveredNotifications];//3.删除未推送的通知请求[center removeAllPendingNotificationRequests];
}#pragma mark - 移除指定通知
- (IBAction)removeSingleNotification:(id)sender {//1.创建通知中心UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];//2.删除指定identifer的已经发送的通知[center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];//3.删除指定identifer未发送的同志请求[center removeDeliveredNotificationsWithIdentifiers:@[@"localNotification"]];
}
Extension
的target,直接选择应用程序的target即可