Local notification on application termination

会有一股神秘感。 提交于 2019-11-27 05:05:45

You will not receive any notice when it gets terminated, when your app is suspended in the background.

iOS will send a kill -9 signal for your apps progress and you app is just killed, this is the same thing that happens when the user kills your app from the quicklaunch tray.

From the Apple documentation:

Even if you develop your app using iOS SDK 4 and later, you must still be prepared for your app to be killed without any notification. The user can kill apps explicitly using the multitasking UI. In addition, if memory becomes constrained, the system might remove apps from memory to make more room. Suspended apps are not notified of termination but if your app is currently running in the background state (and not suspended), the system calls the applicationWillTerminate: method of your app delegate. Your app cannot request additional background execution time from this method.

Applications can create a local notification at a "future" date and time which could be used to notify the user that the application was terminated. If they then tap the application they can then restart your app.

This is working in my app that uses/requires Bluetooth Central in the info.plist (so it will run in the background). I assume that you will have configured your application to run in the background in your info.plist as well.

- (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];
    }

}

I was having the same issue as yourself. However, I discovered if I didn't set a fireDate it started to work.

- (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
}

I must warn you though, it doesn't work on an empty application. It does work on my application which has plenty of views in memory, therefore it must be related to the amount of time it takes to dealloc its memory.

W

From the app double press on home button and swipe to kill app always calls applicationWillTerminate. If you do single press and send the app in background and then double press again to swipe and kill the app, applicationWillTerminate may not be called every time.

- (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.
}

I was able to schedule local notification in the appWillTerminate, using below code:

    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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!