iOS set local notification on a specific day

前端 未结 4 1217
终归单人心
终归单人心 2021-01-21 02:17

I am sure this question is duplicated somewhere, but I can\'t find a solution. I am making an app in which one feature allows the user to select the days and times they will rec

4条回答
  •  梦毁少年i
    2021-01-21 03:20

    I have also searched about it. Below code work good for me. Pass the week day value 1 to 7 Sunday to Saturday and notification body with action which you want to fire and specify your date then notification will come on that specific day.Hope this help you.

    -(void) weekEndNotificationOnWeekday: (int)weekday :(UILocalNotification *)notification : (NSDate*) alramDate
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
        [componentsForFireDate setWeekday: weekday] ; //for fixing Sunday
      //   [componentsForFireDate setHour: 20] ; //for fixing 8PM hour
     //    [componentsForFireDate setMinute:0] ;
     //    [componentsForFireDate setSecond:0] ;
        notification.repeatInterval = NSWeekCalendarUnit;
        notification.fireDate=[calendar dateFromComponents:componentsForFireDate];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    
    }
    

提交回复
热议问题