Ios &引用;WatchKit模拟器动作“;无法在实际的apple watch设备上工作

Ios &引用;WatchKit模拟器动作“;无法在实际的apple watch设备上工作,ios,push-notification,apple-watch,Ios,Push Notification,Apple Watch,我已经测试了模拟的apple watch推送通知,它运行良好 然而,当我试图将它发送到实际的苹果手表时,按钮没有出现。。。为什么会这样 当我以json格式而不是文本发送推送通知时,它不会振动或发出嘟嘟声 { “aps”:{ “警报”:{ “正文”:“很棒!\n新输入”, “标题”:“可选标题” }, “类别”:“散漫” }, “WatchKit模拟器动作”:[ { “标题”:“详情”, “标识符”:“sDetailsButtonAction” } ], “自定义密钥”:"使用此文件可定义通知的

我已经测试了模拟的apple watch推送通知,它运行良好

然而,当我试图将它发送到实际的苹果手表时,按钮没有出现。。。为什么会这样

当我以json格式而不是文本发送推送通知时,它不会振动或发出嘟嘟声

{
“aps”:{
“警报”:{
“正文”:“很棒!\n新输入”,
“标题”:“可选标题”
},
“类别”:“散漫”
},
“WatchKit模拟器动作”:[
{
“标题”:“详情”,
“标识符”:“sDetailsButtonAction”
}
],
“自定义密钥”:"使用此文件可定义通知的测试负载。aps字典指定类别、警报文本和标题。WatchKit Simulator Actions数组可以提供一个或多个操作按钮的信息,以及标准的Dismise按钮。任何其他顶级键都是自定义负载。如果您的应用程序中有多个这样的JSON文件项目中,您可以在选择调试Watch应用程序的通知界面时选择它们。“
}
看一看

若要指定自定义操作按钮,需要创建自定义通知类别。创建通知时,请将类别设置为自定义类别

苹果文档中的示例如下:

func registerSettingsAndCategories() {
    var categories = NSMutableSet()

    var acceptAction = UIMutableUserNotificationAction()
    acceptAction.title = NSLocalizedString("Accept", comment: "Accept invitation")
    acceptAction.identifier = "accept"
    acceptAction.activationMode = UIUserNotificationActivationMode.Background
    acceptAction.authenticationRequired = false

    var declineAction = UIMutableUserNotificationAction()
    declineAction.title = NSLocalizedString("Decline", comment: "Decline invitation")
    declineAction.identifier = "decline"
    declineAction.activationMode = UIUserNotificationActivationMode.Background
    declineAction.authenticationRequired = false

    var inviteCategory = UIMutableUserNotificationCategory()

    inviteCategory.setActions([acceptAction, declineAction], forContext: UIUserNotificationActionContext.Default)
    inviteCategory.identifier = "invitation"
    categories.addObject(inviteCategory)

// Configure other actions and categories and add them to the set...

    var settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)

    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}
从以前的awnser:

1) 在appDelegate的父应用中注册类别:

- (void)registerSettingsAndCategories {
// Create a mutable set to store the category definitions.
NSMutableSet* categories = [NSMutableSet set];

// Define the actions for a meeting invite notification.
UIMutableUserNotificationAction* acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.title = NSLocalizedString(@"Repondre", @"Repondre commentaire");
acceptAction.identifier = @"respond";
acceptAction.activationMode = UIUserNotificationActivationModeForeground; //UIUserNotificationActivationModeBackground if no need in foreground.
acceptAction.authenticationRequired = NO;

// Create the category object and add it to the set.
UIMutableUserNotificationCategory* inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
[inviteCategory setActions:@[acceptAction]
                forContext:UIUserNotificationActionContextDefault];
inviteCategory.identifier = @"respond";

[categories addObject:inviteCategory];

// Configure other actions and categories and add them to the set...

UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:
                                        (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound)
                                                                         categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];}
2) 从您的Apns服务器添加类别(对于我来说是“响应”)

3) 在WatchKitExtension中,传递了以下数据:

 - (void)handleActionWithIdentifier:(NSString *)identifier  forRemoteNotification:(NSDictionary *)remoteNotification{

     if ([identifier isEqualToString:@"respond"]) {
//Do stuff Here to handle action... 
     }}
4) 在父应用程序的appDelegate中:

- (void)       application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}
警告!您也必须在您的父应用程序中处理此操作(因为当您向下平移通知时,响应按钮在iphone上也将可见。在:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

我应该把它放在哪里?它在func应用程序中吗(应用程序:UIApplication,DidReceiveMemotentification用户信息:[NSObject:AnyObject]){?我试图发送这个json,并在应用程序收到通知时设置registerSettingsAndCategories。但该按钮仍然没有出现在apple watch上:{“aps”:{“badge”:1,“alert”“:”您的消息“,”声音“:”cat.caf“,”类别“:”respond“}}这属于启动时执行的代码中的某个地方(例如,您的didFinishLaunchingWithOptions)。然后,您可以在计划通知时,将通知的“类别”属性设置为自定义类别。有一个关于如何创建自定义通知操作(=按钮)的详细教程请注意,除了模拟器之外,您不需要任何JSON。这只是UILocalNotification对象的文本表示。明白了!将其放在“didFinishLaunchingWithOptions”中而且它是有效的!请注意,在实际设备上,您必须发送一个真正的推送通知进行测试,您不能使用通知负载,因为它在您的方案中是灰色的。。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {