WatchKit Simulator Actions Not Working on Real Apple Watch

I have tested simulated apple apple viewing notification and it works great ...

However, when I tried to send it to the actual apple clock, the button doesn't appear ... why is that?

And it doesn't vibrate or sound when I send a json push notification instead of text ...

{
    "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."
}
      

Run codeHide result


enter image description here

+3


source to share


2 answers


Check it out .

To specify custom action buttons, you need to create a special notification category. When you create a notification, set the category to your own.



Example from Apple documentation:

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)
}

      

+6


source


From the previous ten: How to handle action buttons in push notifications

1) Register the category in your 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) From your Apns server add a category (for me, "answer")

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

      

3) In your WatchKitExtention, you have data passed to:



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

     if ([identifier isEqualToString:@"respond"]) {
//Do stuff Here to handle action... 
     }}

      

4) In the Parent appDelegate:

- (void)       application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}

      

Attention! you will have to handle this action in your Parent app too (because the Respond button will be visible on the iphone as well when the notification is panned.

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

      

+1


source







All Articles