简体   繁体   中英

Show local notification on specific day and specific time in iOS

i want to show local notification on every saturday like this,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat=@"EEEE";
        NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
        NSLog(@"day: %@", dayString);

        if([dayString isEqualToString:@"Saturday"])
        {
            NSLog(@"Success");
            [self PushNotification];
        }
-(void)PushNotification
{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertTitle = @"Test";
        localNotification.alertBody =@"test of notification";


        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
        [componentsForFireDate setWeekday: 7]; //for fixing Saturday
        [componentsForFireDate setHour: 17]; //for fixing 5PM hour
        [componentsForFireDate setMinute:0];
        [componentsForFireDate setSecond:0];

        localNotification.repeatInterval = NSCalendarUnitWeekOfMonth; 
    }

but my local notification is display in every minit then how can i display notification on every Saturday of the week. thanks.

Try to declare fireData property in localNotification and schedule your notification

localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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