我知道强制退出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();
}
如果应用程序已强制退出,我是否遗漏了任何内容或无法从操作中调用代码。如果是这样,有一种简单的方法可以阻止动作按钮在该场景中可见?
答案 0 :(得分:0)
正如评论者所述,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];
}