view.addSubview(scrollView) } } applicationIconBadgeNumber UIApplication 的applicationIconBadgeNumber属性被废弃,建议使用UNUserNotificationCenter.current { // iOS17之后设置角标,需要先授权 // UNUserNotificationCenter.current().setBadgeCount(10) UNUserNotificationCenter.current().setBadgeCount(10) { error in if let error {
systemVersion] floatValue] < 10.0) { } else { //如果是iOS 10以上版本则必须执行以下操作 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate [alertView show]; } } //iOS10新增:处理前台收到通知的代理方法 - (void)userNotificationCenter:(UNUserNotificationCenter UNNotificationPresentationOptionAlert); } //iOS10新增:处理后台点击通知的代理方法 -(void)userNotificationCenter:(UNUserNotificationCenter
相关类库介绍 UserNotifications 模块主要涉及到以下一些类库 用户通知中心 UNUserNotificationCenter 主要管理通知相关的调度,添加,其相关的属性和方法如下 // for the current application open class func current() -> UNUserNotificationCenter // 请求通知的权限 @available(iOS 10.0, *) optional public func userNotificationCenter(_ center: UNUserNotificationCenter @available(iOS 10.0, *) optional public func userNotificationCenter(_ center: UNUserNotificationCenter If you have obtained this attachment from UNUserNotificationCenter then the URL will be security-scoped
将UNNotificationRequest添加到UNUserNotificationCenter。 案例 申请授权(异步操作)。 -> Bool { // 请求通知权限 UNUserNotificationCenter.current() .requestAuthorization(options: -> Bool { // 请求通知权限 UNUserNotificationCenter.current() .requestAuthorization NSObject, UNUserNotificationCenterDelegate { // 前台展示通知 func userNotificationCenter(_ center: UNUserNotificationCenter -> Bool { // 设置代理 UNUserNotificationCenter.current().delegate = notificationHandler
content.body = @" 不能为空"; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter
*center = [UNUserNotificationCenter currentNotificationCenter]; //必须写代理,不然无法监听通知的接收与点击事件 center.delegate before the application returns from applicationDidFinishLaunching:. - (void)userNotificationCenter:(UNUserNotificationCenter - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse 推送请求添加到推送管理中心(UNUserNotificationCenter)中 1、新功能trigger可以在特定条件触发,有三类:UNTimeIntervalNotificationTrigger、 * center = [UNUserNotificationCenter currentNotificationCenter]; // 将通知请求 add 到 UNUserNotificationCenter
content.body = @" 不能为空"; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter
iOS 10以上系统的实现 iOS 10以前使用UILocalNotification,iOS开始支持一个新的类库UNUserNotificationCenter,都给了他特定的前缀UN了,可见重视程度 UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 注册通知 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; UNMutableNotificationContent 然后就可以处理接收通知: #pragma mark - UNUserNotificationCenterDelegate // iOS 10收到通知 - (void)userNotificationCenter:(UNUserNotificationCenter
currentDevice] systemVersion] floatValue] >= 10.0) { // 如果检查到时iOS 10以上版本则必须执行以下操作 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate nil]; [alertView show]; } } // iOS 10新增: 处理前台收到通知的代理方法 - (void)userNotificationCenter:(UNUserNotificationCenter UNNotificationPresentationOptionAlert); } //iOS10新增:处理后台点击通知的代理方法 -(void)userNotificationCenter:(UNUserNotificationCenter
要在iOS系统中使用通知,必须获取到用户权限,UserNotification框架中申请通知用户权限需要通过UNNotificationCenter来完成,示例如下: //进行用户权限的申请 [[UNUserNotificationCenter /在block中会传入布尔值granted,表示用户是否同意 if (granted) { //如果用户权限申请成功,设置通知中心的代理 [UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]]; [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObjects:category, nil]]; [[UNUserNotificationCenter _WATCHOS_AVAILABLE(3.0); /* 这个方法当接收到通知后,用户点击通知激活app时被调用,无论前台还是后台 */ - (void)userNotificationCenter:(UNUserNotificationCenter
User Notifications Framework类关系图 重点介绍: UNUserNotificationCenter通知中心,用以管理通知的注册、权限获取和管理、通知的删除与更新,通过代理分发事件等 UNNotification 通知实体,在UNUserNotificationCenter的代理回调事件中,告知App接收到一条通知,包含一个发起通知的请求UNNotificationRequest UNNotificationRequest super viewDidLoad]; if ([[UIDevice currentDevice] systemVersion].floatValue >= 10.0) { UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; requestWithIdentifier:@"KFGroupNotification" content:content trigger:trigger]; // 4、将请求加入通知中心 [[UNUserNotificationCenter
相关代码如下: #import <UserNotifications/UserNotifications.h> …… [[UNUserNotificationCenter currentNotificationCenter 每个Request对象都需要配置一个id来标识该条推送内容,UNUserNotificationCenter通过该id来管理(包括增加、删除、查询和修改)所有的Request。 requestWithIdentifier:@"testId" content:content trigger:calendarTrigger]; //将推送请求添加到管理中心才会生效 UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; [center addNotificationRequest:request [PageSwitch handlePushSwitch:params]; } } //用户点击推送消息的回调 - (void)userNotificationCenter:(UNUserNotificationCenter
@param center 、 @param response 、 @param completionHandler 、 */ - (void)userNotificationCenter:(UNUserNotificationCenter requestWithIdentifier:@"NotificationDefault" content:content trigger:trigger]; //添加通知请求 [[ UNUserNotificationCenter didReceiveLocalNotification:(UILocalNotification *)notification iOS 10 之后 - (void)userNotificationCenter:(UNUserNotificationCenter fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler ios 10之后 - (void)userNotificationCenter:(UNUserNotificationCenter
UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]]; //注册通知 UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter]; //设置代理,用于检测点击方法 center.delegate 收到通知(本地和远端) UNUserNotificationCenterDelegate //当APP处于前台的时候收到通知的事件 - (void)userNotificationCenter:(UNUserNotificationCenter
audioAsset.duration; float audioDurationSeconds = CMTimeGetSeconds(audioDuration); UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent
audioAsset.duration; float audioDurationSeconds = CMTimeGetSeconds(audioDuration); UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; UNMutableNotificationContent
接收到通知消息首先会调用极光的这个代理 #pragma mark- JPUSHRegisterDelegate // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse 在后台的时候 // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse completionHandler(); // 系统要求执 这个 法 } 在前台的时候 // iOS 10 Support - (void)jpushNotificationCenter:(UNUserNotificationCenter
else { method_exchangeImplementations(originalMethod, swizzledMethod); } } @implementation UNUserNotificationCenter
content.body = @" 不能为空"; UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter
floatValue] >= 10.0) { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self;