gpt4 book ai didi

ios - 强制退出后未调用交互式通知处理程序

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

我知道强制退出后application:didReceiveRemoteNotification不会被调用(如Will iOS launch my app into the background if it was force-quit by the user?中所述),但我也注意到application:handleActionWithIdentifier:也没有被调用。我的交互式通知仍在向用户显示,但是单击操作不会导致打开应用程序(就像单击通知本身一样)或运行application:handleActionWithIdentifier:中定义的代码。当用户选择操作时,通知消失,给他们的印象是实际上所有工作都在消除该通知,而一切正常。这似乎是非常差的用户体验,所以如果iOS在没有采取任何措施的情况下继续提供通知措施,我会感到惊讶。我希望我做错了什么:

//application:didFinishLaunchingWithOptions:
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"Safe"];
[action1 setIdentifier:NotificationSafeActionIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];

UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];
[action2 setTitle:@"Unsafe"];
[action2 setIdentifier:NotificationUnsafeActionIdent];
[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];

//application:handleActionWithIdentifier:
if ([identifier isEqualToString:NotificationSafeActionIdent]) {
// handle safe here including http call
} else if ([identifier isEqualToString:NotificationUnsafeActionIdent]) {
// Handle unsafe here including http call
}

if (completionHandler) {
completionHandler();
}

如果应用被强制退出,我是否遗漏任何东西,或者无法从操作中调用代码。如果是这样,是否有一种简单的方法可以防止在这种情况下显示操作按钮?

最佳答案

如评论者所述,当用户强制退出应用程序时,将调用applicationWillTerminate:。如果您在返回之前迅速将本地通知排队,则会显示您的消息,就像Lookout一样:

- (void)applicationWillTerminate:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = NSLocalizedString(@"MyAppName has terminated. Please reopen the app to enable [whatever].", @"");
notification.soundName = UILocalNotificationDefaultSoundName;
[application cancelAllLocalNotifications]; // Don't let these messages stack
[application presentLocalNotificationNow:notification];
}

关于ios - 强制退出后未调用交互式通知处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28946921/

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