如何处理推送通知中的操作按钮

时间:2015-04-07 06:12:32

标签: ios iphone push-notification watchkit apple-watch

使用Apple Watch通知: - 因此,如果我在通知界面中添加按钮(来自对象Lib),那么错误是:

通知界面中不支持按钮

PushNotificationPayload.apnsWatchKit Simulator Actions喜欢这个:

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

并且模拟器向我显示

enter image description here

现在我的问题是,如何从服务器发送View时处理此PushNotification按钮,

如果aps文件包含操作按钮是Apple Watch的唯一选项,

如何使用指定的密钥从通知字典中的服务器发送它?

如何更改操作按钮BG颜色?

请有人向我提供包含aps设备ActionButton的示例the Apple Watch文件,而不是Simulator

我只是通过将WatchKit Simulator Actions键更改为WatchKit Actions键来测试,但它没有显示操作按钮。

正如@vivekDas在答案中所建议的那样,我通过将aps替换为:

来检查
"alert" : {
     "body" : "Acme message received from Johnny Appleseed",
     "action-loc-key" : "VIEW",
     "action" : [
        {
           "id" : "buttonTapped",
           "title" : "View"
        }
     ]
  }

但Xcode中的模拟器确实显示了一个动作按钮。

我认为这可以在设备Apple Watch上运行,这是......?

你对此有什么建议。

2 个答案:

答案 0 :(得分:17)

我已经在这两个小时内苦苦挣扎,所以这就是我通过真正的APNS Serv为生产中的通知按钮做的事情,

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服务器添加类别(对我而言#34;回复")

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

答案 1 :(得分:-3)

使用Json Payload,如下所示

{
    "aps": {
         "badge": 10,
         "alert": "Your message",
         "sound": "cat.caf"
    }
}

检查此苹果文档链接https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html

通知有效负载部分。