Watchkit动态通知按钮自定义

Watchkit dynamic notification button customization

我希望在我的动态通知中自定义关闭按钮和通过 PushNotificationPayload.apns 通过 "WatchKit Simulator Actions" 发送的按钮。有人可以告诉我我忽略了什么吗?

另外,如何处理按下按钮的功能?理想情况下,我想将信息发送回父应用程序。

回答第一个问题:无法自定义关闭按钮或通过 PushNotificationPayload.apns 添加的按钮。由系统提供。

回答第二个问题: 在你的 PushNotificationPayload.apns 你有这样的东西:

"WatchKit Simulator Actions": [
    {
        "title": "Button 1",
        "identifier": "button1Action",
    },
    {
        "title": "Button 2",
        "identifier": "button2Action",
    }
],

然后在你的主界面控制器中,实现这个方法

- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification
{
    // Detect button identifier, decide which method to run
}

但是,请记住,如果您打算在实际设备上使用 LocalNotification,则需要实施

- (void)handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)localNotification;

希望对您有所帮助。

编辑

如果您想将数据发送到 iPhone 应用,请使用

openParentApplication

接口控制器的方法class。

为了补充 lvp 的回答,我已经详细说明了在 iphone parent 应用程序中注册新类别的过程:(需要生产 apns)

How to Handle Action Buttons in Push Notifications

****编辑****

在简历中:

你必须从服务器 APNS 发送一个类别(给我回复):

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

在您 parent 的应用程序 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];}

并在您的 WatchKit Extention 中处理此问题:

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

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