Local notification on application termination

2020-01-27 06:45发布

I am working on an application which will not work if terminated. It has some background tasks. I want to show a local notification if the app is terminated. There are applications which do this which means this is doable. But I am not able to find out a way.

I have tried to set up a local notification in applicationWillTerminate: method of appdelegate as well as added a notification of app termination in my viewcontroller but none of the methods get called when app is actually terminated.

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

Any help would be great.

Thanks in Advance !!!

5条回答
ら.Afraid
2楼-- · 2020-01-27 06:53

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.

查看更多
Root(大扎)
3楼-- · 2020-01-27 06:56

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.
}
查看更多
我想做一个坏孩纸
4楼-- · 2020-01-27 07:00

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()
查看更多
等我变得足够好
5楼-- · 2020-01-27 07:05

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

}
查看更多
看我几分像从前
6楼-- · 2020-01-27 07:19

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

查看更多
登录 后发表回答