【问题标题】:UILocalNotification fire when i open the notification tray to see the notification当我打开通知托盘查看通知时 UILocalNotification 触发
【发布时间】:2012-07-26 07:46:31
【问题描述】:

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

【问题讨论】:

    标签: iphone objective-c ios


    【解决方案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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多