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

14 iphone push-notification ios apple-watch watchkit

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

通知界面中不支持按钮

PushNotificationPayload.apnsWatchKit Simulator Actions这样的:

"WatchKit Simulator Actions": 
[
    {
        "title": "View",
        "identifier": "firstButtonAction"
    }
],
Run Code Online (Sandbox Code Playgroud)

和模拟器向我显示这个

在此输入图像描述

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

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

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

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

会不会有人请给我的样本aps文件,其中包括ActionButton对设备the Apple Watch不为Simulator

我只是通过将WatchKit Simulator ActionsKey 更改为Key来测试,WatchKit Actions但它没有显示Action Button.

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

"alert" : {
     "body" : "Acme message received from Johnny Appleseed",
     "action-loc-key" : "VIEW",
     "action" : [
        {
           "id" : "buttonTapped",
           "title" : "View"
        }
     ]
  }
Run Code Online (Sandbox Code Playgroud)

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

我想这可能会在Device上运行Apple Watch,这是......?

你对此有什么建议?

Jer*_*tti 18

我一直在努力工作两个小时,所以这就是我通过真正的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];
}
Run Code Online (Sandbox Code Playgroud)

2)从您的Apns服务器添加类别(对我来说"回复")

{"aps":{"alert":"bla","category":"respond","badge":2}}
Run Code Online (Sandbox Code Playgroud)

3)在WatchKitExtention中,您传入了数据:

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

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

4)在您的父应用程序的appDelegate中:

- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}
Run Code Online (Sandbox Code Playgroud)

警告 !你必须在你的父应用程序中处理这个动作(因为当你平移通知时,响应按钮也会在iphone上显示.):

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
Run Code Online (Sandbox Code Playgroud)


viv*_*Das -3

使用 Json Payload 如下所示

{
    "aps": {
         "badge": 10,
         "alert": "Your message",
         "sound": "cat.caf"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

通知负载部分。