如何处理推送通知中的操作按钮 [英] How to Handle Action Buttons in Push Notifications

查看:18
本文介绍了如何处理推送通知中的操作按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Working on Apple Watch Notifications:- so if i add button in Notification Interface (from object Lib) then the error is :

通知界面不支持按钮

PushNotificationPayload.apnsWatchKit Simulator Actions 像这样:

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

和模拟器向我展示这个

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

Now my Question is, How can I Handle this View Button when send a PushNotification from server,

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

If the aps file contain the action button is the only option for Apple Watch,

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

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

有人请给我包含 ActionButton 的示例 aps 文件,用于设备 Apple Watch 而不是 Simulator代码>

Will anyone please give me the sample aps file that include ActionButton for Device the Apple Watch not for the Simulator

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

I just test by changing the WatchKit Simulator Actions Key to WatchKit Actions Key but it shows no Action Button.

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

As suggested by @vivekDas in Answer, I checked by replacing in the aps as :

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

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

but simulator in Xcode does show an action button.

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

I think this may run on Device Apple Watch, is this...?

你有什么建议给我.

推荐答案

我已经为此苦苦挣扎了两个小时,所以这就是我通过真正的 APNS Serv 在生产中处理通知按钮的方式,

I've been struggling with this for two Hours, so this is how I do for notifications buttons in production, via real APNS Serv,

1) 在 appDelegate 的父应用中注册类别:

1) Register for the category in your appDelegate's parent app :

- (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 服务器添加类别(对我来说响应")

2) From your Apns server add the category (for me "respond")

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

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

3) In your WatchKitExtention, you have the data passed in :

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

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

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

4) In your Parent app's appDelegate :

- (void) application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
     forRemoteNotification:(NSDictionary *)userInfo
         completionHandler:(void (^)())completionHandler {
    completionHandler();
}

警告!您也必须在您的父应用程序中处理此操作(因为当您向下平移通知时,响应按钮也将在 iphone 上可见.在:

Warning ! you'll have to handle this action too in your Parent app ( because the respond button will be visible too on iphone when you pan down the notification. in the :

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

这篇关于如何处理推送通知中的操作按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆