简体   繁体   English

iPhone- iOS本地通知与滑动到期

[英]iPhone- ios localnotification with sliding expiration

Can i create a Local notfication in an iphone app with sliding expiration. 我可以在具有到期日的iPhone应用程序中创建本地通知吗?

My problem statement is that i want to show user a local notification in case the app is terminated by IOS or by an exception. 我的问题陈述是,如果应用程序被IOS或异常终止,我想向用户显示本地通知。

So to achieve this i am trying to do the following 因此,要实现这一点,我正在尝试执行以下操作

Create a thread that runs every 5 seconds. 创建一个每5秒运行一次的线程。 This thread first cancels all scheduled notification and then schedules a notification with currenttime +10 seconds. 该线程首先取消所有计划的通知,然后计划当前时间+10秒的通知。

This way if the app gets killed then the user will be notified within 10 seconds that the app is terminated. 这样,如果应用被杀死,则将在10秒内通知用户该应用已终止。

Please advise 请指教

Thanks,Amit 谢谢,阿米特

Implement the below method... 实现以下方法...

- (void)applicationWillTerminate:(UIApplication *)application {

    // Cancel all local notification.

    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

   // Create an future date with 10+ second

    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setSecond:10];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *fireDate = [calendar dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; 

    // Scheduled the notification

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    [localNotification setFireDate:fireDate];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
    [localNotification setAlertBody:@"Application Terminated"];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

UPDATE UPDATE

From the Apple Doc Apple Doc

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. 对于不支持后台执行或与iOS 3.x或更早版本链接的应用程序,总是在用户退出应用程序时调用此方法。 For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. 对于支持后台执行的应用程序,用户退出应用程序时通常不会调用此方法,因为在这种情况下,应用程序只是移至后台。 However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason . 但是, 在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可以调用此方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM