gpt4 book ai didi

ios - 交互式推送通知 - 隐藏/显示按钮

转载 作者:行者123 更新时间:2023-11-28 09:55:31 26 4
gpt4 key购买 nike

我关注了this tutorial在推送通知上显示按钮。
通过在 didFinishLaunchingWithOptions 中调用 registerNotification 来注册按钮。
然而,在少数情况下,我需要显示一个没有任何按钮的简单通知。如何显示/隐藏不同通知的按钮?

最佳答案

要添加另一种没有按钮的交互式通知,您必须更新 UIUserNotificationSettings

创建一个没有任何按钮的新通知类别UIMutableUserNotificationCategory:

UIMutableUserNotificationCategory *newNotificationCategory = [[UIMutableUserNotificationCategory alloc] init];
newNotificationCategory.identifier = @"no_button_id";

然后,将这个新类别添加到现有的UIUserNotificationSettings:

    NSMutableArray *arrNewCategories = [NSMutableArray new];
UIUserNotificationSettings *oldSettings = [[UIApplication sharedApplication]currentUserNotificationSettings];
for (UIMutableUserNotificationCategory *oldCategory in oldSettings.categories)
{
if (![oldCategory.identifier isEqualToString:newNotificationCategory.identifier])
[arrNewCategories addObject:oldCategory];
}
[arrNewCategories addObject:newNotificationCategory];

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *newSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:[NSSet setWithArray:arrNewCategories]];

[[UIApplication sharedApplication] registerUserNotificationSettings:newSettings];

只需确保 newNotificationCategory 的标识符与您的 UILocalNotification 的类别匹配,您不需要任何按钮。

然后安排通知:

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:afireDate];
localNotification.alertBody = @"alert body text";
localNotification.category = @"no_button_id"; // Same as category identifier
localNotification.timeZone = [NSTimeZone systemTimeZone];
localNotification.soundName = SOUND_FILE;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];

关于ios - 交互式推送通知 - 隐藏/显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982802/

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