【问题标题】:Schedule local Notification after app was closed for 7 days应用程序关闭 7 天后安排本地通知
【发布时间】:2013-03-06 16:08:09
【问题描述】:

为我的应用程序启用local notifications 相当困难。

是的,我用 local notification 女巫设置了我的项目,我想在应用程序关闭 一周 周后安排时间。例如,如果用户在 8 号星期六打开应用程序,本地通知应该会在下一个星期六 15 号出现,但时间应该改变。例如,他/她在晚上 8 点关闭应用程序,我不想在下周晚上 8 点打扰他们,所以我想在下午 6 点或类似的时间显示每个通知。

现在您知道我的问题了,这是我正在使用的代码:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
    NSDateComponents *componentsForReferenceDate = [[NSDateComponents alloc] init];

    //set day (saturday)

    [componentsForReferenceDate setDay:26] ;
    [componentsForReferenceDate setMonth:1] ;
    [componentsForReferenceDate setYear:2013] ;

    [componentsForReferenceDate setHour: 17] ;
    [componentsForReferenceDate setMinute:30] ;
    [componentsForReferenceDate setSecond:00] ;

    NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForReferenceDate];

    // Create the notification

    UILocalNotification *notification = [[UILocalNotification alloc]  init] ;


    notification.fireDate = fireDateOfNotification ;
    notification.timeZone = [NSTimeZone localTimeZone] ;
    notification.alertBody = [NSString stringWithFormat: @"Du wirst vermisst! \nDeine App braucht dich, schreibe sie zu Ende!"] ;
    notification.alertAction = @"Zurückkehren";
    notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
    notification.repeatInterval= NSWeekCalendarUnit ;
    notification.soundName = @"Appnotifisound.wav";
    notification.applicationIconBadgeNumber = 1;
    [[UIApplication sharedApplication] scheduleLocalNotification:notification] ;

}

我知道必须有更多的方法来删除徽章和didrecievenotification,但我将它们放了出来,因为对此很重要。

使用此代码,我设法将通知安排在每个星期六下午 5:30(德国)。但我只想安排一次,当应用程序已关闭恰好 一周 周时。这有可能吗?如果有人可以更正我的代码或为此提供解决方案,我会很高兴。

最好的问候,感谢您阅读这篇长文,

诺亚

【问题讨论】:

    标签: iphone ios notifications uilocalnotification


    【解决方案1】:

    您应该取消安排之前的通知。

    for (UILocalNotification *lNotification in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
        {
            if ([[lNotification.userInfo valueForKey:@"information"] isEqualToString:@"Some information"])
            {
                [[UIApplication sharedApplication]cancelLocalNotification:lNotification];
                break;
            }
        }
    

    通过以下方式设置开火日期:

    NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *componentsForReferenceDate = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
                                         fromDate:[NSDate date]];
        [componentsForReferenceDate setHour: 17] ;
        [componentsForReferenceDate setMinute:30] ;
        [componentsForReferenceDate setSecond:00] ;
    
        NSDate *tempDate = [calendar dateFromComponents: componentsForReferenceDate];
        [componentsForReferenceDate release];
    
        NSDateComponents *comps = [[NSDateComponents alloc]init];
        [comps setDay:7];
        NSDate *fireDateOfNotification = [calendar dateByAddingComponents:comps
                                                                   toDate:tempDate options:0]
        [comps release];
    

    【讨论】:

    • 之前的通知被取消了!看方法中的第一行代码,还是我搞错了?
    • 您正在取消所有本地通知。所以我认为,它是一样的。否则你可以把它放在函数的开头。
    • 你为什么要为日、月和年设置日期组件?它的问题。
    • 这是什么意思?我在stackoverflow.com/questions/13204419/… 之前就已经设置好了,他们告诉我要使用它!但我想在下午 6 点关闭 7 天后展示它!这可能吗?
    【解决方案2】:

    notification.repeatInterval = NSWeekCalendarUnit 导致了这种情况。使用:

    notification.repeatInterval = 0;
    

    这可以防止重复 (source)。

    【讨论】:

    • 我知道,但是如果日期是过去,这会显示通知吗?
    • @MasterRazer 是的,对不起。已更新。
    • 是的,我知道你的意思!看第二行代码有一行写着: [[UIApplication sharedApplication] cancelAllLocalNotifications];这不是解决方案吗?
    • :D 哈哈这是苹果iOS造成的吗?
    • 我无法想象这是怎么回事?!
    【解决方案3】:

    这里是 Swift 2.0 适配

    取消安排当前通知

        for notifMe:AnyObject in UIApplication.sharedApplication().scheduledLocalNotifications!{
            let title = notifMe.userInfo["information"] as? String
            if title == "some information"{
                    UIApplication.sharedApplication().cancelLocalNotification(notifMe as! UILocalNotification)
            }
            }
    

    设置通知日期

        let date = NSDate()
        let calendar = NSCalendar.currentCalendar()
        let components = calendar.components(NSCalendarUnit.Year.union(NSCalendarUnit.Month).union(NSCalendarUnit.Day),fromDate: date)
        components.hour = 17
        components.minute = 30
        components.second = 00
    
        let tempDate = calendar.dateFromComponents(components)!
        let comps = NSDateComponents()
        comps.day = 7
        let fireDateOfNotification = calendar.dateByAddingComponents(comps, toDate: tempDate, options:[])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多