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

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

通知接口中不支持按钮

PushNotificationPayload.apns有像这样的WatchKit Simulator Actions

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

模拟器向我展示了这一点

在此处输入图像描述

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

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

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

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

有人请给我一个示例aps文件,其中包括ActionButton for Device the Apple Watch而不是Simulator

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

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

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

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

我认为这可能会在Device Apple Watch上运行,这是……?

你对此有什么建议?

我一直在努力工作两个小时,所以这就是我通过真正的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服务器添加类别(对我来说“回复”)

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

使用Json Payload,如下所示

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

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

通知有效负载部分。