“WatchKit Simulator Actions”不适用于实际的 Apple Watch 设备

Posted

技术标签:

【中文标题】“WatchKit Simulator Actions”不适用于实际的 Apple Watch 设备【英文标题】:"WatchKit Simulator Actions" not working on actual apple watch device 【发布时间】:2015-05-15 08:37:21 【问题描述】:

我已经测试了模拟的 Apple Watch 推送通知,它工作正常...

但是,当我尝试将其发送到实际的 Apple Watch 时,按钮没有出现……为什么会这样?

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


    "aps": 
        "alert": 
            "body": "Great!\nNew input",
            "title": "Optional title"
        ,
        "category": "sCategory"
    ,
    
    "WatchKit Simulator Actions": [
                                   
                                   "title": "Details",
                                   "identifier": "sDetailsButtonAction"
                                   
                                   ],
    
    "customKey": "Use this file to define a testing payload for your notifications. The aps dictionary specifies the category, alert text and title. The WatchKit Simulator Actions array can provide info for one or more action buttons in addition to the standard Dismiss button. Any other top level keys are custom payload. If you have multiple such JSON files in your project, you'll be able to select them when choosing to debug the notification interface of your Watch App."

【问题讨论】:

【参考方案1】:

看看this。

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

Apple 文档中的示例:

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)

【讨论】:

我应该把它放在哪里?是在 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) ? 我尝试发送此 json 并在应用收到通知时设置 registerSettingsAndCategories。但是该按钮仍然没有出现在 Apple Watch 上: "aps": "badge": 1, "alert": "Your message", "sound": "cat.caf","category":"respond " 这属于您在启动时执行的代码中的某个位置(例如您的 didFinishLaunchingWithOptions)。然后,您可以在安排通知时,将通知的类别属性设置为您的自定义类别,有一个关于如何创建自定义通知操作的详细教程(=按钮)on this page 请注意,除了模拟器,您不需要任何 JSON。这只是 UILocalNotification 对象的文本表示。 知道了!把它放在“didFinishLaunchingWithOptions”里面就可以了!【参考方案2】:

来自以前的遮阳篷:How to handle action buttons in push notifications

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 服务器添加类别(对我来说是“响应”)

"aps":"alert":"bla","category":"respond","badge":2

3) 在您的 WatchKitExtention 中,您有传入的数据:

 - (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 

【讨论】:

请注意,在实际设备上,您必须发送真正的推送通知进行测试,您不能使用通知有效负载,因为它在您的方案中是灰色的..

以上是关于“WatchKit Simulator Actions”不适用于实际的 Apple Watch 设备的主要内容,如果未能解决你的问题,请参考以下文章