【问题标题】:How to Handle Action Buttons in Push Notifications如何处理推送通知中的操作按钮
【发布时间】:2015-04-07 06:12:31
【问题描述】:

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

通知界面不支持按钮

PushNotificationPayload.apnsWatchKit Simulator Actions 像这样:

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

模拟器会向我展示这个

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

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

如何通过指定的Key从服务器发送通知字典?

如何更改操作按钮的背景颜色?

谁能给我样品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 上运行,这是...?

您对此有何建议。

【问题讨论】:

  • 您无法在模拟器中查看推送通知。
  • 嘿有同样的问题...你设法解决了吗?

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


【解决方案1】:

我已经为此苦苦挣扎了两个小时,所以这就是我在生产中通过真正的 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 {

【讨论】:

  • 他不是要求这个.... 他想在 Apple Watch(设备)上的推送通知中显示一个按钮。您提供的 json 示例没有操作按钮
  • 这正是代码正在做的事情。一旦您注册了类别(第 1 步),第 2 步中的 json 在我的示例“Repondre”中显示按钮这是在实际设备上显示按钮的方式(没有 watchkit 模拟器操作,这不起作用真实设备。)
  • 知道了,谢谢!您的解决方案有效....被 Xcode 中提供的“WatchKit Simulator Actions”json 混淆了。
  • 没问题..“WatchKit Simulator Actions”一开始也让我感到困惑......我一开始在我的 aps 服务器中尝试了 WatchKit Actions,因为这就是这样做的方法..
  • @JeremyLuisetti: 在handleActionWithIdentifier 方法中 //Do stuff here... 我需要做什么才能呈现接口控制器?我试过 [self pushControllerWithName...] 但这不是一个选项,reloadRootControllerWithNames ...只是打开手表应用程序,它不会转到我想要的特定页面
【解决方案2】:

使用下面给出的 Json Payload

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

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

通知负载部分。

【讨论】:

  • 点击“查看按钮”后,您将在 UIApplication 委托方法“- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo”中获得回调处理动作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 2023-03-10
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多