3

我使用了本地通知并安排了触发日期,但是当应用程序在后台并且我打开通知托盘查看通知时,本地通知会自动触发但触发日期仍然存在..有什么解决方案可以解决这个问题

4

1 回答 1

1

这听起来像你有两个问题。首先,本地通知是在过去设置的触发日期创建的 - 这就是为什么它会在您打开应用程序时立即出现。

其次,您可能将通知的 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];
于 2012-12-05T10:38:43.200 回答