Schedule local Notification after app was closed f

2019-04-10 01:48发布

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

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

}

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.

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,

Noah

3条回答
祖国的老花朵
2楼-- · 2019-04-10 02:05

The notification.repeatInterval = NSWeekCalendarUnit is causing this. use:

notification.repeatInterval = 0;

This prevent repeating (source).

查看更多
该账号已被封号
3楼-- · 2019-04-10 02:06

Here is the Swift 2.0 adaptation

Unscheduling the current notification

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

Setting the date for the notification

    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:[])
查看更多
地球回转人心会变
4楼-- · 2019-04-10 02:30

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];
查看更多
登录 后发表回答