iOS10 推送最新特性研究

2020-01-17 21:54:51于丽

添加通知 / 更新通知

 1.创建一个UNNotificationRequest类的实例,一定要为它设置identifier, 在后面的查找,更新, 删除通知,这个标识是可以用来区分这个通知与其他通知
 2.把request加到UNUserNotificationCenter, 并设置触发器,等待触发
 3.
如果另一个request具有和之前request相同的标识,不同的内容, 可以达到更新通知的目的


  NSString *requestIdentifer = @"TestRequest";
  UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifer content:content trigger:trigger1];
  //把通知加到UNUserNotificationCenter, 到指定触发点会被触发
  [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  }];

 //在另外需要更新通知的地方
UNMutableNotificationContent *newContent = [[UNMutableNotificationContent alloc] init];
newContent.title = @"Update";
newContent.subtitle = @"XXXXXXXXX";
newContent.body = @"Copyright © 2016年 jpush. All rights reserved.";
UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
 UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"TestRequest" content:newContent trigger:trigger1];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {

}];

 iOS10,推送

获取和删除通知

这里通知是有两种状态
 •Pending 等待触发的通知
 •Delivered 已经触发展示在通知中心的通知


 //获取未触发的通知
[[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
  NSLog(@"pending: %@", requests);
}];

//获取通知中心列表的通知
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
  NSLog(@"Delivered: %@", notifications);
}];

 //清除某一个未触发的通知
 [[UNUserNotificationCenter currentNotificationCenter] removePendingNotificationRequestsWithIdentifiers:@[@"TestRequest1"]];
 //清除某一个通知中心的通知
 [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[@"TestRequest2"]];
 //对应的删除所有通知
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
[[UNUserNotificationCenter currentNotificationCenter] removeAllDeliveredNotifications];