iOS本地通知不显示操作按钮问题排查
iOS本地通知分类操作按钮不显示排查
我在iOS应用中用Objective-C编写了本地通知调度代码,给通知设置了categoryIdentifier为"demoCategory",日志显示该分类已存在,但触发通知时,预先添加到该分类的两个操作按钮并未显示。请问是categoryIdentifier未生效,还是需要通过其他方式验证分类是否正确应用?
调度通知代码
- (void) schedule:(CDVInvokedUrlCommand*)command { NSArray* notifications = command.arguments; [self.commandDelegate runInBackground:^{ for (NSDictionary* options in notifications) { APPNotificationContent* notification; notification = [[APPNotificationContent alloc] initWithOptions:options]; notification.title = @"This is title2"; notification.subtitle = @"This is subtitle"; notification.body = @"This is body"; notification.categoryIdentifier = @"demoCategory"; NSLog(@"categoryIdentifier: %@", notification.categoryIdentifier); NSString* found; found = [_center hasActionGroup:notification.categoryIdentifier] ? @"yes" : @"no"; NSLog(@"category existance: %@", found); [self scheduleNotification:notification]; } [self check:command]; }]; }
触发通知时的日志
2022-12-09 13:11:12.595804+1300 MyApp[71182:11636137] NotificationCenter Handle push from foreground 2022-12-09 13:11:12.595994+1300 MyApp[71182:11636137] Notification received 2022-12-09 13:11:12.596147+1300 MyApp[71182:11636137] Push Plugin key: alert 2022-12-09 13:11:12.626052+1300 MyApp[71182:11637337] categoryIdentifier: demoCategory 2022-12-09 13:11:12.627402+1300 MyApp[71182:11637337] category existance: yes
排查方向与验证方法
1. 确认分类注册时机与配置正确性
hasActionGroup:仅能验证分类是否被注册到通知中心,无法保证分类的操作按钮配置正确,也不代表通知真正关联了该分类。需重点确认:
- 分类必须在**应用启动阶段(比如
didFinishLaunchingWithOptions:)**完成注册,不能晚于通知调度的时机。如果注册滞后,通知无法关联到分类。 - 分类创建时需正确添加操作按钮,示例代码如下:
// 创建操作按钮 UNNotificationAction *action1 = [UNNotificationAction actionWithIdentifier:@"action1" title:@"按钮1" options:UNNotificationActionOptionNone]; UNNotificationAction *action2 = [UNNotificationAction actionWithIdentifier:@"action2" title:@"按钮2" options:UNNotificationActionOptionNone]; // 创建分类 UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"demoCategory" actions:@[action1, action2] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]; // 注册到通知中心 [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:[NSSet setWithObject:category]];
2. 验证自定义内容到系统通知的映射
APPNotificationContent是自定义类,需确保它的categoryIdentifier属性正确传递给系统UNMutableNotificationContent。检查scheduleNotification:方法内部,是否完成了赋值:
- (void)scheduleNotification:(APPNotificationContent *)notificationContent { UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.title = notificationContent.title; content.subtitle = notificationContent.subtitle; content.body = notificationContent.body; // 关键:必须将自定义分类ID赋值给系统通知内容 content.categoryIdentifier = notificationContent.categoryIdentifier; // 创建触发条件与请求 UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"demoNotification" content:content trigger:trigger]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:nil]; }
如果这里漏了赋值,即使自定义类的字段正确,系统通知也不会关联分类。
3. 确认通知展示场景
- 若应用处于前台,默认不会展示通知横幅,需在通知代理方法中指定展示选项,才能显示操作按钮:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { // 允许横幅展示并显示操作按钮 completionHandler(UNNotificationPresentationOptionBanner | UNNotificationPresentationOptionList); }
只有当通知以横幅/列表形式展示时,操作按钮才会出现;前台静默接收通知不会显示按钮。
4. 清除缓存测试
旧的分类缓存可能导致异常,删除应用后重新安装,确保分类注册逻辑从头执行。
内容的提问来源于stack exchange,提问作者Terry Windwalker
相关产品推荐
相关产品推荐

