这篇文章使用ObjectC编写,官网中有Swift的写法。
User Notifications | Apple Developer Documentation
1.获取通知权限
//注册通知点击处理事件
UNUserNotificationCenter.currentNotificationCenter.delegate = self;
//(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"通知权限已获取。");
}
}];
2.发送通知
- (void)sendNotify {
NSLog(@"发送通知");
// 定义通知操作
UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"collect" title:@"收藏" options:UNNotificationActionOptionNone];
UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"like" title:@"喜欢" options:UNNotificationActionOptionNone];
// 定义通知分类
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"myCategory" actions:@[action1, action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
// 将通知操作和分类注册到通知中心
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObject:category]];
// 创建本地通知的内容
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
//通知标题
content.title = @"My Notification";
//通知主体
content.body = @"This is a notification with buttons.";
content.sound = [UNNotificationSound defaultSound];
NSDictionary *userInfo = @{@"state":content.body};
content.userInfo = userInfo; // 将参数存储在 userInfo 字典中
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"notify_img" ofType:@"jpeg"];
NSLog(@"图片路径:%@",imagePath);
// 将图像文件附加到本地通知中
NSError *error = nil;
UNNotificationAttachment *attachment = [UNNotificationAttachment attachmentWithIdentifier:@"imageAttachment" URL:[NSURL fileURLWithPath:imagePath] options:nil error:&error];
if (error) {
NSLog(@"Failed to create attachment: %@", error);
} else {
content.attachments = @[attachment];
NSLog(@"插入图片");
}
// 指定堆叠标识符和应用程序的自定义通知标识符和相应的分类标识符
NSString *notificationGroupIdentifier = @"myNotificationGroup";
NSString * identifierForVendor = [[UIDevice currentDevice].identifierForVendor UUIDString];
NSLog(@"通知ID:%@",identifierForVendor);
NSString *notificationIdentifier = identifierForVendor;
NSString *groupTitle = @"Group Title";
content.categoryIdentifier = @"myCategory";
content.threadIdentifier = notificationGroupIdentifier;
if (@available(iOS 12.0, *)) {
content.summaryArgument = groupTitle;
}
// 创建本地通知,并在指定的时间提示用户
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:notificationIdentifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Failed to add notification: %@", error.localizedDescription);
}
}];
}
实现点击事件,还需要实现本地通知触发事件的方法
// 实现 UNUserNotificationCenterDelegate 协议方法,该方法在触发本地通知操作时调用
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
// 确定是使用哪个操作将本地通知与应用程序相关联
NSString *actionIdentifier = response.actionIdentifier;
// 检查本地通知的类别,并根据需要采取适当的行动
NSString *categoryIdentifier = response.notification.request.content.categoryIdentifier;
// 读取传递的信息
NSDictionary<NSString *, id> *userInfo = response.notification.request.content.userInfo;
NSString *state = [userInfo valueForKey:@"statement"];
if ([categoryIdentifier isEqualToString:@"myCategory"]) {
if ([actionIdentifier isEqualToString:@"action1"]) {
// 在按钮 1 上单击后需要执行的操作
NSLog(@"点击收藏按钮,数据为:%@",state);
} else if ([actionIdentifier isEqualToString:@"action2"]) {
// 在按钮 2 上单击后需要执行的操作
NSLog(@"点击喜欢按钮,数据为:%@",state);
}else{
NSLog(@"直接打开通知,数据为:%@",state);
}
}
// 完成处理程序以指示已处理通知响应
completionHandler();
}
3.实现效果
(1)通知分类、
(2)通知增加按钮、点击事件
(3)为通知增加图片
(4)每分钟自动发送通知,就算app退出之后也可以按时发送
(5)添加了图片附件(长按通知展示)
Ps:也可以实现app右上角展示红点标记通知数量,修改通知的徽章数量即可。
效果图如下: