gpt4 book ai didi

ios - 实现接收推送通知操作的委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 12:14:08 25 4
gpt4 key购买 nike

我已经为我的报警项目创建了一个本地推送通知。

问题是,当我单击一个操作按钮(暂停和确定)时,我实现的委托(delegate)不会被调用。

这是我推送本地通知的代码:

UIMutableUserNotificationAction *notificationAction1 = [[UIMutableUserNotificationAction alloc] init];
notificationAction1.identifier = Snooze;
notificationAction1.title = @"Snooze";
notificationAction1.activationMode = UIUserNotificationActivationModeBackground;
notificationAction1.destructive = NO;
notificationAction1.authenticationRequired = NO;

UIMutableUserNotificationAction *notificationAction2 = [[UIMutableUserNotificationAction alloc] init];
notificationAction2.identifier = Okay;
notificationAction2.title = @"Okay";
notificationAction2.activationMode = UIUserNotificationActivationModeBackground;
notificationAction2.destructive = NO;
notificationAction2.authenticationRequired = NO;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Alarm";
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[notificationAction1,notificationAction2] forContext:UIUserNotificationActionContextMinimal];

NSSet *categories = [NSSet setWithObjects:notificationCategory, nil];

下面是实现委托(delegate)的代码:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {    
if ([identifier isEqualToString:Snooze]) {

NSLog(@"You chose Snooze");
}
else if ([identifier isEqualToString:Okay]) {

[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSLog(@"You chose Okay");
}
if (completionHandler) {

completionHandler();
}
}

我还不知道如何让小睡和好按钮工作,所以我只是记录了一些东西,但当我点击小睡/好时没有记录任何东西。我错过了什么吗?

最佳答案

请检查下面的代码,它可能对你有帮助..

NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
NSString * const NotificationActionOneIdent = @"ACTION_ONE";
NSString * const NotificationActionTwoIdent = @"ACTION_TWO";

- (void)registerForNotification {

UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"Action 1"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:@"Action 2"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];

UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:@[action1, action2]
forContext:UIUserNotificationActionContextDefault];

NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationType types = (UIUserNotificationTypeAlert|
UIUserNotificationTypeSound|
UIUserNotificationTypeBadge);

UIUserNotificationSettings *settings;
settings = [UIUserNotificationSettings settingsForTypes:types
categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

UIApplicationDelegate 协议(protocol)有 2 个新方法。

1) application:handleActionWithIdentifier:forLocalNotification:completionHandler:
2) application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

当用户从您的推送通知中选择一个操作时,将在后台调用这些方法。

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

if ([identifier isEqualToString:NotificationActionOneIdent]) {

NSLog(@"You chose action 1.");
}
else if ([identifier isEqualToString:NotificationActionTwoIdent]) {

NSLog(@"You chose action 2.");
}
if (completionHandler) {

completionHandler();
}
}

Source Link

关于ios - 实现接收推送通知操作的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32389852/

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