iOS|iOS 本地通知实现
1、导入所需的 库
#import
2、注册通知,代理实现
// 代理
@interface AppDelegate () @end// 注册通知- (void)registerNotification {
UNAuthorizationOptions options = UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge;
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 允许授权
} else {
// 不允许授权
}
}];
// 获取用户对通知的设置
// 通过settings.authorizationStatus 来处理用户没有打开通知授权的情况
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"%@",settings);
}];
}// 在前台时 收到通知
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
}// 点击通知,从后台进入前台
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSString *identifier =response.actionIdentifier;
if ([identifier isEqualToString:@"open"]) {
NSLog(@"打开操作");
} else if ([identifier isEqualToString:@"close"]) {
NSLog(@"关闭操作");
}
completionHandler();
}
3、添加通知、包含一些操作
- (void)addNotification {
// 创建一个通知内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.badge = @1;
content.title = @"title";
content.subtitle = @"subtitle";
content.body = @"body";
content.sound = [UNNotificationSound defaultSound];
content.categoryIdentifier = @"category";
// 通知触发器
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:false];
// 通知请求
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"noti" content:content trigger:trigger];
//添加通知
[UNUserNotificationCenter.currentNotificationCenter addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {NSLog(@"error:%@",error);
}];
// 添加通知的一些操作
UNNotificationAction *openAction = [UNNotificationAction actionWithIdentifier:@"open" title:@"打开" options:UNNotificationActionOptionForeground];
UNNotificationAction *closeAction = [UNNotificationAction actionWithIdentifier:@"close" title:@"关闭" options:UNNotificationActionOptionDestructive];
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"category" actions:@[openAction, closeAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
NSSet *sets = [NSSet setWithObject:category];
[UNUserNotificationCenter.currentNotificationCenter setNotificationCategories:sets];
}
文章图片
效果图.png 4、移除通知
- (void)removeNotifiation {
// 移除 待处理的通知
[UNUserNotificationCenter.currentNotificationCenter removePendingNotificationRequestsWithIdentifiers:@[@"noti"]];
// 移除 已经处理的通知
[UNUserNotificationCenter.currentNotificationCenter removeDeliveredNotificationsWithIdentifiers:@[@"noti"]];
// 移除所有的通知
[UNUserNotificationCenter.currentNotificationCenter removeAllDeliveredNotifications];
[UNUserNotificationCenter.currentNotificationCenter removeAllPendingNotificationRequests];
}
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 不舍
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- Flutter的ListView
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密