Watchkit动态通知按钮自定义

时间:2015-02-12 17:17:21

标签: ios objective-c watchkit

我正在通过动态通知中的“WatchKit Simulator Actions”自定义解除按钮和我通过PushNotificationPayload.apns发送的按钮。有人可以告诉我我在忽视什么吗?

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

2 个答案:

答案 0 :(得分:3)

回答第一个问题:无法自定义关闭按钮,也无法通过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

InterfaceController类的方法。

答案 1 :(得分:3)

作为对lvp的回答的补充,我详细说明了在你那里注册新类别的程序:(生产支持需求)

How to Handle Action Buttons in Push Notifications

**** **** EDIT

简历:

从服务器APNS,你必须发送一个类别(对我来说回应):

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

在您的父母应用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... 
     }}