即使在将相同的线程标识符设置为 UNMutableNotificationContent 之后,UNUserNotificationCenter 也不会将通知组合在一起
UNUserNotificationCenter doesn't club together the notifications even after setting the same thread identifier to the UNMutableNotificationContent
我正在为 iOS 10 生成本地通知。以下是我的示例代码。
UNUserNotificationCenter *nCentre = [UNUserNotificationCenter currentNotificationCenter];
[nCentre requestAuthorizationWithOptions:(UNAuthorizationOptionBadge +
UNAuthorizationOptionAlert +
UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Title";
content.subtitle = @"Subtitle";
content.body = @"Body Message";
content.categoryIdentifier = kCategoryOne;
content.threadIdentifier = @"123";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:kNotiIdOne
content:content
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
[nCentre setDelegate:self];
UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc] init];
content2.title = @"Title";
content2.subtitle = @"Subtitle";
content2.body = @"Body Message";
content2.categoryIdentifier = kCategoryTwo;
content2.threadIdentifier = @"123";
UNNotificationRequest *request2 = [UNNotificationRequest requestWithIdentifier:kNotiIdTwo
content:content2
trigger:[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0f repeats:false]];
[nCentre addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
UNMutableNotificationContent *content3 = [[UNMutableNotificationContent alloc] init];
content3.title = @"Title";
content3.subtitle = @"Subtitle";
content3.body = @"Body Message";
content3.categoryIdentifier = kCategoryThree;
content3.threadIdentifier = @"123";
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:kNotiIdThree
content:content3
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request3
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
我添加了 7 个这样的通知。
但是通知中心的通知并没有聚集在一起。
代码中是否缺少某些内容?
这个解决方案对我有用;
首先,我保证threadIdentifier是一样的:
if (@available(iOS 11.0, *)) {
UNTextInputNotificationAction *action = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:UNNotificationActionOptionNone textInputButtonTitle:@"Send" textInputPlaceholder:@""];
UNNotificationCategory *showTitleCategory = [UNNotificationCategory categoryWithIdentifier:@"actionCategory" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionHiddenPreviewsShowTitle];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:showTitleCategory]];
content.threadIdentifier = @"LocalNotifications";
content.categoryIdentifier = @"actionCategory";
}
第二,保持 requestIdentifier 的唯一性,在我的例子中我这样做了:
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:*someRandomString* content:content trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
就是这样。
我正在为 iOS 10 生成本地通知。以下是我的示例代码。
UNUserNotificationCenter *nCentre = [UNUserNotificationCenter currentNotificationCenter];
[nCentre requestAuthorizationWithOptions:(UNAuthorizationOptionBadge +
UNAuthorizationOptionAlert +
UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = @"Title";
content.subtitle = @"Subtitle";
content.body = @"Body Message";
content.categoryIdentifier = kCategoryOne;
content.threadIdentifier = @"123";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:kNotiIdOne
content:content
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
[nCentre setDelegate:self];
UNMutableNotificationContent *content2 = [[UNMutableNotificationContent alloc] init];
content2.title = @"Title";
content2.subtitle = @"Subtitle";
content2.body = @"Body Message";
content2.categoryIdentifier = kCategoryTwo;
content2.threadIdentifier = @"123";
UNNotificationRequest *request2 = [UNNotificationRequest requestWithIdentifier:kNotiIdTwo
content:content2
trigger:[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.0f repeats:false]];
[nCentre addNotificationRequest:request2 withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
UNMutableNotificationContent *content3 = [[UNMutableNotificationContent alloc] init];
content3.title = @"Title";
content3.subtitle = @"Subtitle";
content3.body = @"Body Message";
content3.categoryIdentifier = kCategoryThree;
content3.threadIdentifier = @"123";
UNNotificationRequest *request3 = [UNNotificationRequest requestWithIdentifier:kNotiIdThree
content:content3
trigger:[UNTimeIntervalNotificationTrigger
triggerWithTimeInterval:5.0f
repeats:false]];
[nCentre addNotificationRequest:request3
withCompletionHandler:^(NSError * _Nullable error) {
if (error)
{
NSLog(@"error: %@",error.description);
}
}];
我添加了 7 个这样的通知。
但是通知中心的通知并没有聚集在一起。 代码中是否缺少某些内容?
这个解决方案对我有用;
首先,我保证threadIdentifier是一样的:
if (@available(iOS 11.0, *)) {
UNTextInputNotificationAction *action = [UNTextInputNotificationAction actionWithIdentifier:@"Reply" title:@"Reply" options:UNNotificationActionOptionNone textInputButtonTitle:@"Send" textInputPlaceholder:@""];
UNNotificationCategory *showTitleCategory = [UNNotificationCategory categoryWithIdentifier:@"actionCategory" actions:@[action] intentIdentifiers:@[] options:UNNotificationCategoryOptionHiddenPreviewsShowTitle];
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:showTitleCategory]];
content.threadIdentifier = @"LocalNotifications";
content.categoryIdentifier = @"actionCategory";
}
第二,保持 requestIdentifier 的唯一性,在我的例子中我这样做了:
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:*someRandomString* content:content trigger:nil];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"add NotificationRequest succeeded!");
}
}];
就是这样。