在应用关闭7天后安排本地通知 [英] Schedule local Notification after app was closed for 7 days

查看:79
本文介绍了在应用关闭7天后安排本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

It was quite a bit hard to enable local notifications for my application.

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

And yeah, I set up my project with a local notification witch I want to schedule after the app had been closed for one week. For example if the user opens the app on saturday 8th, the local notification should appear on the next saturday the 15th, but the time should changed. For example he/she closes the app at 8pm and I don't want to disturb them at 8pm next week so I want to display every notification at 6pm or something like that.

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

Now you know my problem and here is my code I'm using:

- (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的方法,但是我将它们删除了,因为对此不重​​要.

I know that there have to be more methods for deleting the badge and the didrecievenotification, but I let them out because there are not important for this.

借助此代码,我设法将通知安排在每天(星期六)下午 5:30 (德国).但是,我只打算安排一次,即该应用已完全关闭一个周.有可能吗?如果有人可以更正我的代码或为此提供解决方案,我将非常高兴.

With this code I managed to schedule the notification on every saturday at 5:30pm (Germany). But I wanted to schedule it only once, when the app had been closed for exactly one week. Is that somehow possible? I would be glad if someone could correct my code or give me a solution for this.

最诚挚的问候,谢谢您阅读这篇长文章,

Best regards and thank you for reading this long post,

诺亚

推荐答案

您应取消安排先前的通知.

You should unschedule previous notification.

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

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

Set fire date by below way:

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];

这篇关于在应用关闭7天后安排本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆