gpt4 book ai didi

ios - UNNotificationAction 不显示操作

转载 作者:行者123 更新时间:2023-11-29 11:47:48 27 4
gpt4 key购买 nike

我有以下显示通知的代码。它的工作,但是,它没有显示操作按钮。我想知道可能出了什么问题?

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];


UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];

// Objective-C
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Don't forget";
content.body = @"Buy some milk";
content.sound = [UNNotificationSound defaultSound];

// Time
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:30
repeats:NO];

// Objective-C
NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];


// Actions
UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Snooze"
title:@"Snooze" options:UNNotificationActionOptionNone];
UNNotificationAction *deleteAction = [UNNotificationAction actionWithIdentifier:@"Delete"
title:@"Delete" options:UNNotificationActionOptionDestructive];

// Objective-C
UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory"
actions:@[snoozeAction,deleteAction] intentIdentifiers:@[]
options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];

// Objective-C
[center setNotificationCategories:categories];

// Objective-C
content.categoryIdentifier = @"UYLReminderCategory";

最佳答案

在通过设置 notificationCategoriescategoryIdentifier 完全配置 UNMutableNotificationContent 之前,您创建了 UNNotificationRequest。 p>

只需将以下行移至代码末尾即可解决您的问题:

NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
}
}];

而且由于模拟器不支持3D Touch,最好在真机上测试一下。当您看到通知时,用力触摸它,您应该会看到操作按钮。

关于ios - UNNotificationAction 不显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42713256/

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