我已经苦斗了两个小时,以下是我在生产环境中通过真实的APNS服务处理通知按钮的方法:
1)在您的appDelegate的父应用程序中注册该类别:
- (void)registerSettingsAndCategories {
NSMutableSet* categories = [NSMutableSet set];
UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.title = NSLocalizedString(@"Repondre", @"Repondre commentaire");
acceptAction.identifier = @"respond";
acceptAction.activationMode = UIUserNotificationActivationModeForeground;
acceptAction.authenticationRequired = NO;
UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
[inviteCategory setActions:@[acceptAction]
forContext:UIUserNotificationActionContextDefault];
inviteCategory.identifier = @"respond";
[categories addObject:inviteCategory];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:
(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
2)从您的APNS服务器添加类别(对于我来说是“respond”)
{"aps":{"alert":"bla","category":"respond","badge":2}}
3)在您的WatchKit扩展程序中,您传递的数据为:
- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification{
if ([identifier isEqualToString:@"respond"]) {
}
}
4) 在您的主应用程序的 appDelegate 中:
- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
completionHandler();
}
警告!您还需要在您的父级应用程序中处理此操作(因为当您向下滑动通知时,在iPhone上也会看到响应按钮)。在:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {