gpt4 book ai didi

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

转载 作者:技术小花猫 更新时间:2023-10-29 10:16:25 25 4
gpt4 key购买 nike

处理 Apple Watch 通知:- 因此,如果我在通知界面(来自对象库)中添加按钮,则错误为:

通知界面不支持按钮

PushNotificationPayload.apnsWatchKit Simulator Actions 像这样:

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

模拟器向我展示了这个

enter image description here

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

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

如何使用指定的Key从Notification Dictionary中的服务器发送?

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

谁能给我示例 aps 文件,其中包含用于设备 Apple Watch 而不是用于 Simulator ActionButton

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

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

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

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

我认为这可能会在设备 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();
}

警告!你也必须在你的父应用程序中处理这个 Action (因为当你向下平移通知时,响应按钮也会在 iPhone 上可见。在:

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

关于ios - 如何处理推送通知中的操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29484951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com