But even if I added negative and positive actions according to the document, the data parsed by the receiving end still only contains negative behaviors,just like default behavior.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self setupNotificationCategories];
return YES;
}
- (void)setupNotificationCategories {
// 创建一个简单的动作
UNNotificationAction *actionAccept = [UNNotificationAction actionWithIdentifier:@"ACCEPT_ACTION"
title:@"ACCEPT"
options:UNNotificationActionOptionNone];
UNNotificationAction *actionDecline = [UNNotificationAction actionWithIdentifier:@"DECLINE_ACTION"
title:@"DECLINE"
options:UNNotificationActionOptionDestructive];
// 创建一个通知类别,包含上面的动作
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"MEETING_INVITATION"
actions:@[actionAccept,actionDecline] // 可以添加多个动作
intentIdentifiers:@[] // 可以添加意图标识符,用于处理Siri等
options:UNNotificationCategoryOptionCustomDismissAction];
// 将类别添加到通知中心以便使用
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center setNotificationCategories:[NSSet setWithObject:category]];
}-(void)postLocalNotificationWithMessage:(NSString*)message identifier:(NSString*)identifier level:(int)level{
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// 处理授权结果
if (granted) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Notification Tag";
content.body = @"您有一条新消息";
content.categoryIdentifier = @"MEETING_INVITATION"; // 关键:绑定分类
content.userInfo = @{@"MEETING_ID" : @"meeting1",
@"USER_ID" : @"user1" };
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy/MM/dd HH:mm";
NSString *date = [formatter stringFromDate:[NSDate date]];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:2 repeats:NO];
// 生成唯一标识符
NSString *uniqueID = [NSString stringWithFormat:@"dynaTagGroupNotification_%@_%@_%@", identifier, date, @(arc4random_uniform(1000))];
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:uniqueID
content:content
trigger:trigger];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
// 成功添加
}
}];
};
}];
}The relevant code is as follows: