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

15

在处理苹果手表通知时:如果我在通知界面中(来自对象库)添加按钮,则会出现以下错误:

通知界面不支持按钮

PushNotificationPayload.apns 中包含类似于 WatchKit Simulator Actions 的内容:

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

并且模拟器向我展示了这个

enter image description here

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

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

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

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

是否可以给我提供一个包括设备Apple Watch而不是SimulatorActionButton的样本aps文件?

我尝试通过将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上运行,是吗?

您对此有什么建议?


1
模拟器中无法检查推送通知。 - vivekDas
嘿,我也遇到了同样的问题... 你解决了吗? - user1872384
3个回答

18

我已经苦斗了两个小时,以下是我在生产环境中通过真实的APNS服务处理通知按钮的方法:

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服务器添加类别(对于我来说是“respond”)

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

3)在您的WatchKit扩展程序中,您传递的数据为:

- (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示例没有操作按钮。 - user1872384
1
这段代码的作用是完全相同的。一旦您注册了类别(第一步),在第二步中的JSON将显示我的示例中的按钮“Repondre”。这是在实际设备上显示按钮的方法(不需要WatchKit模拟器操作,因为这在真实设备上无法工作)。 - Jeremy Luisetti
1
没问题。 "WatchKit Simulator Actions" 一开始也让我感到困惑... 我最初在我的应用程序服务器中尝试了 WatchKit Actions,认为那是做这件事的方法。 - Jeremy Luisetti
@JeremyLuisetti:在handleActionWithIdentifier方法中 //在这里做一些事情...我需要做什么来呈现一个界面控制器?我尝试了[self pushControllerWithName...],但那不是一个选项,而reloadRootControllerWithNames...只是打开了手表应用程序,没有进入我想要的特定页面。 - MayNotBe
为此,我会使用“presentControllerWithName”,但要注意,“handleActionWithIdentifier”只在主界面控制器中执行... - Jeremy Luisetti
显示剩余2条评论

0
func userNotificationCenter(_ center: UNUserNotificationCenter,
       didReceive response: UNNotificationResponse,
       withCompletionHandler completionHandler: 
         @escaping () -> Void) {
       
   // Get the meeting ID from the original notification.
   let userInfo = response.notification.request.content.userInfo
   let meetingID = userInfo["MEETING_ID"] as! String
   let userID = userInfo["USER_ID"] as! String
        
   // Perform the task associated with the action.
   switch response.actionIdentifier {
   case "ACCEPT_ACTION":
      sharedMeetingManager.acceptMeeting(user: userID, 
                                    meetingID: meetingID)
      break
        
   case "DECLINE_ACTION":
      sharedMeetingManager.declineMeeting(user: userID, 
                                     meetingID: meetingID)
      break
        
   // Handle other actions…
 
   default:
      break
   }
    
   // Always call the completion handler when done.    
   completionHandler()
}

-3

当点击“查看按钮”时,您将在UIApplication委托方法“-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo”中收到回调,您必须在其中处理该操作。 - vivekDas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接