申请终止的本地通知

Swa*_*ati 13 iphone uiapplicationdelegate uilocalnotification

我正在开发一个应用程序,如果终止它将无法工作.它有一些后台任务.如果应用程序被终止,我想显示本地通知.有些应用程序执行此操作意味着这是可行的.但我无法找到方法.

我试图在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:.
}
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒.

提前致谢 !!!

Joh*_*ger 11

应用程序可以在"未来"日期和时间创建本地通知,该日期和时间可用于通知用户应用程序已终止.如果他们然后点击应用程序,他们就可以重新启动您的应用

这在我的应用程序中工作,该应用程序在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];
    }

}
Run Code Online (Sandbox Code Playgroud)


rck*_*nes 5

当应用终止在后台挂起时,您将不会收到任何通知。

iOS会向您的应用进度发送kill -9信号,而您的应用只是被杀死,这与用户从快速启动托盘中杀死您的应用时发生的情况相同。

Apple文档中

即使您使用iOS SDK 4和更高版本开发应用程序,也必须做好准备以在没有任何通知的情况下终止您的应用程序。用户可以使用多任务UI明确终止应用程序。此外,如果内存不足,系统可能会从内存中删除应用程序以腾出更多空间。挂起的应用程序不会收到终止通知,但如果您的应用程序当前正在后台运行(而不是挂起),则系统将调用应用程序委托的applicationWillTerminate:方法。您的应用无法通过此方法请求额外的后台执行时间。


Wil*_*rge 5

我和你有同样的问题。然而,我发现如果我没有设置 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
}
Run Code Online (Sandbox Code Playgroud)

但我必须警告您,它不适用于空应用程序。它确实适用于我的应用程序,该应用程序在内存中有大量视图,因此它必须与释放内存所需的时间有关。

  • 是正确的。您可以简单地在 applicationWillTerminate 中添加 sleep(2) : (3认同)