gpt4 book ai didi

ios - 交互式通知中的操作未反射(reflect)在应用程序中

转载 作者:行者123 更新时间:2023-12-01 16:38:32 26 4
gpt4 key购买 nike

我正在做一个使用 ios 8 交互式通知功能的示例应用程序。就显示通知而言,一切正常。但是当我在通知中选择一个 Action 时,会调用该 Action 的相应方法,但该方法中的代码不会反射(reflect)在应用程序中。

我的应用程序委托(delegate)实现通知的部分代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupNotification];
// Override point for customization after application launch.
return YES;
}

-(void)setupNotification
{
UIMutableUserNotificationAction *accept =[[UIMutableUserNotificationAction alloc]init];
accept.identifier =@"Accepted";
accept.title=@"Accept";
accept.destructive=NO;
accept.authenticationRequired=YES;
accept.activationMode=UIUserNotificationActivationModeBackground;

UIMutableUserNotificationAction *decline =[[UIMutableUserNotificationAction alloc]init];
decline.identifier =@"Rejected";
decline.title=@"Decline";
decline.destructive=YES;
decline.authenticationRequired=YES;
decline.activationMode=UIUserNotificationActivationModeBackground;

UIMutableUserNotificationCategory *actions =[[UIMutableUserNotificationCategory alloc]init];
[actions setIdentifier:@"customActions"];
[actions setActions:@[decline,accept] forContext:UIUserNotificationActionContextDefault];

NSSet* categories = [NSSet setWithArray:@[actions]];
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
{
if([notification.category isEqualToString:@"customActions"])
{
ViewController *screen =[[ViewController alloc]init];
if([identifier isEqualToString:@"Rejected"])
{

[screen deleteAction];
}
else if([identifier isEqualToString:@"Accepted"])
{
[screen acceptAction];
}
}

// Important to call this when finished
completionHandler();
}

我的 View Controller
    - (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(void)deleteAction
{
NSLog(@"delete");
[self.view setBackgroundColor:[UIColor redColor]];

}
-(void)acceptAction
{
NSLog(@"accept");
[self.view setBackgroundColor:[UIColor greenColor]];
}

- (IBAction)sendNotification:(id)sender {

UILocalNotification* notification = [[UILocalNotification alloc] init];
[notification fireDate ];
[notification setAlertBody:@"Notification triggered"];
[notification setCategory:@"customActions"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

方法内的日志正在打印,但背景颜色没有改变。帮助我,我需要在我现有的应用程序中实现此功能。

最佳答案

迅速添加:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
Swift.print(identifier)
}

在 AppDelegate 中。一旦您按下通知中的操作按钮,它将调用

关于ios - 交互式通知中的操作未反射(reflect)在应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26251090/

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