我正在处理一个如果被终止则无效的应用程序。它有一些后台任务。如果应用程序被终止,我想显示本地通知。有些应用程序执行此操作意味着这是可行的。但我无法找到办法。
我尝试在applicationWillTerminate:appdelegate方法中设置本地通知,并在我的viewcontroller中添加了应用终止的通知,但实际终止app时没有调用任何方法。
- (void)applicationWillTerminate:(UIApplication *)application
{
NSLog(@"terminated");
UIApplication * app = [UIApplication sharedApplication];
NSDate *date = [[NSDate date] dateByAddingTimeInterval:15];
UILocalNotification *alarm = [[UILocalNotification alloc] init] ;
if (alarm) {
alarm.fireDate = [NSDate date];
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.alertBody = @"This app does not work if terminated";
alarm.alertAction = @"Open";
[app scheduleLocalNotification:alarm];
}
[app presentLocalNotificationNow:alarm];
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
任何帮助都会很棒。
提前致谢!!!
答案 0 :(得分:10)
应用程序可以在“未来”日期和时间创建本地通知,该日期和时间可用于通知用户应用程序已终止。如果他们然后点击应用程序,他们就可以重新启动您的应用程序。
这适用于我的应用程序,它在info.plist中使用/需要Bluetooth Central(因此它将在后台运行)。我假设您已将应用程序配置为在info.plist中的后台运行。
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Schedule an alarm here to warn the user that they have terminated an application and if they want to re-activate it.
NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:10]; // set a localnotificaiton for 10 seconds
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
// Create a new notification.
UILocalNotification* alarm = [[UILocalNotification alloc] init];
if (alarm)
{
alarm.fireDate = theDate;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"sonar";
alarm.alertBody =@"Background uploads are disabled. Tap here to re-activate uploads." ;
[app scheduleLocalNotification:alarm];
}
}
答案 1 :(得分:4)
当您的应用在后台暂停时,您将不会收到任何通知。
iOS将为您的应用程序进度发送kill -9信号,您的应用程序刚被杀死,这与用户从快速启动托盘中杀死您的应用时发生的情况相同。
即使您使用iOS SDK 4及更高版本开发应用程序,您仍然必须 准备好让您的应用程序在没有任何通知的情况下被杀死该 用户可以使用多任务UI显式杀死应用程序。此外, 如果内存受限制,系统可能会删除应用程序 记忆,以腾出更多空间。暂停的应用程序不会收到通知 终止,但如果您的应用当前正在后台运行 状态(而不是暂停),系统调用 applicationWillTerminate:您的应用委托的方法。你的应用不能 从此方法请求额外的后台执行时间。
答案 2 :(得分:3)
我和你一样有同样的问题。但是,我发现如果我没有设置fireDate,它就会开始工作。
- (void)applicationWillTerminate:(UIApplication *)application
{
//Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:
#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1){
//Annoy The User - Set a badge
[application setApplicationIconBadgeNumber:1];
//Try and alert the user
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Tracking disabled. Tap to resume.";
notification.soundName = UILocalNotificationDefaultSoundName;
[application scheduleLocalNotification:notification];
}
#endif
}
我必须警告你,它不适用于空应用程序。它适用于我的应用程序,它在内存中有很多视图,因此它必须与释放内存所需的时间相关。
w ^
答案 3 :(得分:0)
从应用程序双击主页按钮并滑动以终止应用程序始终调用 applicationWillTerminate 。如果您单击并在后台发送应用程序,然后再次双击滑动并终止应用程序,则每次都可能无法调用 applicationWillTerminate 。
- (void)applicationWillTerminate:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = [NSDictionary dictionaryWithObject:@"killed.app" forKey:@"Killed"];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2]; // Give some time for firing the notification at least 2 seconds.
notification.alertBody = @"App is killed.";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.timeZone = [NSTimeZone systemTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
sleep(1); // I noticed that adding sleep is required as it makes sure the notification is set before the app exits.
}
答案 4 :(得分:0)
我可以使用以下代码在appWillTerminate中安排本地通知:
let localNotification = UILocalNotification.init()
localNotification.fireDate = Date.init(timeIntervalSince1970: Date().timeIntervalSince1970 + 1.5)
localNotification.timeZone = TimeZone.current
localNotification.repeatInterval = NSCalendar.Unit(rawValue: 0)
localNotification.alertBody = "Did terminate app"
localNotification.soundName = "sonar"
UIApplication.shared.scheduleLocalNotification(localNotification)
// block main thread
DispatchQueue.global().async {
sleep(1)
DispatchQueue.main.sync {
CFRunLoopStop(CFRunLoopGetCurrent())
}
}
CFRunLoopRun()