3
我使用了本地通知并安排了启动日期,但是当应用程序处于后台并且我打开通知托盘查看通知时,本地通知会自动触发,但是火灾日期是剩余的..有任何解决方案来解决这个问题UILocalNotification当我打开通知托盘查看通知时触发
我使用了本地通知并安排了启动日期,但是当应用程序处于后台并且我打开通知托盘查看通知时,本地通知会自动触发,但是火灾日期是剩余的..有任何解决方案来解决这个问题UILocalNotification当我打开通知托盘查看通知时触发
这听起来像你有两个问题。首先,本地通知是在过去设置了一个启动日期的情况下创建的 - 这就是为什么一旦您打开应用程序就会显示它。
其次,您可能会将通知的repeatInterval设置为非零值,这会导致它不止一次出现。
请参见下面的代码来设置一个本地通知火在下午3点:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = @"This is a test alert";
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour: 15];
[comps setMinute: 0];
[comps setSecond: 0];
NSDate *threePM = [currentCalendar dateFromComponents:comps];
// Test if the current time is after three or not:
if(threePM != [threePM earlierDate: [NSDate date]])
{
comps = [[NSDateComponents alloc] init];
[comps setDay: 1];
threePM = [currentCalendar dateByAddingComponents: comps toDate: threePM options: 0];
}
localNotification.fireDate = threePM;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication] scheduleLocalNotification: localNotification];