iOS10通知框架UserNotification理解与应用

2020-01-18 16:20:19于海丽


UNNotificationAction * action = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题1" options:UNNotificationActionOptionNone];
 UNNotificationAction * action2 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题2" options:UNNotificationActionOptionNone];
 UNNotificationAction * action3 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题3" options:UNNotificationActionOptionNone];
 UNNotificationAction * action4 = [UNNotificationAction actionWithIdentifier:@"action" title:@"活动标题4" options:UNNotificationActionOptionNone];
  UNNotificationCategory * category = [UNNotificationCategory categoryWithIdentifier:@"myNotificationCategoryBtn" actions:@[action,action2,action3,action4] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
 UNMutableNotificationContent * content = [UNMutableNotificationContent new];
 content.badge = @1;
 content.body = @"这是iOS10的新通知内容:普通的iOS通知";
 //默认的通知提示音
 content.sound = [UNNotificationSound defaultSound];
 content.subtitle = @"这里是副标题";
 content.title = @"这里是通知的标题";
 content.categoryIdentifier = @"myNotificationCategoryBtn";
 //设置5S之后执行
 UNTimeIntervalNotificationTrigger * trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO];
 UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger];
 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]];
 [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  
 }];

需要注意,系统模板最多支持添加4个用户交互按钮,如下图:

iOS10通知框架,iOS10通知框架UserNotification,iOS10远程推送,iOS10本地推送

八、自定义通知模板UI

        通过前边的介绍,我们发现通过UserNotification框架开发者已经可以完成许多从来很难实现的效果。然而这都不是UserNotification框架最强大的地方,UserNotification框架最强大的地方在于其可以完全自定义通知的UI界面。

        完全自定义通知界面是通过iOS扩展来实现的,首先创建一个新的target,如下图:

iOS10通知框架,iOS10通知框架UserNotification,iOS10远程推送,iOS10本地推送