Local notification on application termination

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

}

Leave a Comment