WatchKit handleActionWithIdentifier : forRemoteNot

2019-05-06 12:58发布

问题:

I have a Simple Static Interface for the apple watch notification as follow:

and in the PushNotificationPayload are as follow:

{
    "aps": {
        "alert": {
            "body": "123You have a new message",
            "title": "myApp"
        },
        "category": "respond"
    },

    "WatchKit Simulator Actions": [
        {
            "title": "View Message",
            "identifier": "viewMsgBtn"
        }
    ],

    "customKey": "customKey"
}

and implement the method in InterfaceController

- (void)handleActionWithIdentifier:(NSString *)identifier
             forRemoteNotification:(NSDictionary *)remoteNotification
{
    NSLog(@"Handling remote notification: %@ with identifier: %@", remoteNotification, identifier);
//    [self.lbTest setText:[NSString stringWithFormat:@"Notification: %@",remoteNotification.description]];
}

and I run the simulator with notification:

the awakeWithContext method in InterfaceController is called, and after clicking the View Message button and it loads to my apple watch app interface.

the willActivate method in InterfaceController is called. but the handleActionWithIdentifier forRemoteNotification is not called... any idea?

回答1:

I was experiencing something similar (in Swift) but then realized I had implemented

handleActionWithIdentifier(identifier: String?, forRemoteNotification remoteNotification: [NSObject : AnyObject], withResponseInfo responseInfo: [NSObject : AnyObject])

instead of

handleActionWithIdentifier(identifier: String?, forRemoteNotification remoteNotification: [NSObject : AnyObject])

The former is for cases where the user provides a text response, which is not applicable here.



回答2:

You need to implement not only A short-look interface(Static Interface) also The Long-Look Interface (Dynamic Interface). This guide is perfect : https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/BasicSupport.html#//apple_ref/doc/uid/TP40014969-CH18-SW1

Basically in your NotificationController.m you have this method:

- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {
    // This method is called when a remote notification needs to be presented.
    // Implement it if you use a dynamic notification interface.
    // Populate your dynamic notification interface as quickly as possible.
    //
    // After populating your dynamic notification interface call the completion block.

    completionHandler(WKUserNotificationInterfaceTypeCustom);
}

When you see the type is WKUserNotificationInterfaceTypeCustom. if you do this populate quickly (< 10 seconds) apple watch show Long-Look Interface (Dynamic interface) that permit implement actions. Otherwise short-look (static interface).

regards.