简体   繁体   English

当我打开通知托盘查看通知时,UILocalNotification会触发

[英]UILocalNotification fire when i open the notification tray to see the notification

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

This sounds like you have two issues. 这听起来像你有两个问题。 First, the local notification has been created with a fire date set in the past - that's why its appearing as soon as you open the app. 首先,创建了本地通知,其中包含过去设置的开火日期 - 这就是为什么它会在您打开应用程序时立即显示的原因。

Secondly, you may be setting the notification's repeatInterval to a non-zero value, which will cause it to come up more than once. 其次,您可能将通知的repeatInterval设置为非零值,这将导致它多次出现。

See the below code for setting a local notification to fire at 3pm: 请参阅以下代码,以便在下午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];

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

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