1

I wanna repeat a local notification message every Sunday and Friday at 1.00 pm.

How would I do that?

For the moment I use this code:

-

Edit:

- (void)applicationDidEnterBackground:(UIApplication *)application {

//Calendar
NSCalendar *gregCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateCompnent = [gregCalendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday fromDate:[NSDate date]];


//[dateCompnent setYear:2015];
//[dateCompnent setMonth:7];
//[dateCompnent setDay:28];
[dateCompnent setWeekday:4];
[dateCompnent setHour:15];
[dateCompnent setMinute:53];

UIDatePicker *dd = [[UIDatePicker alloc] init];
[dd setDate:[gregCalendar dateFromComponents:dateCompnent]];

//Notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
[notification setAlertBody:@"Test Notification"];
[notification setFireDate:dd.date];
[notification setSoundName:@"sound.mp3"];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
}
hantoren
  • 357
  • 2
  • 17

3 Answers3

2

Try this:

-(void) SetNotificationForday:(int)weekday :(UILocalNotification *)notification :(NSDate*) date{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    [componentsForFireDate setWeekday:weekday] ; // Set 1 if you need to fix for Sunday
    [componentsForFireDate setHour: 13] ; // Its Set for fixing 1 PM
    [componentsForFireDate setMinute:0] ;
    [componentsForFireDate setSecond:0] ;
    notification.repeatInterval = NSWeekCalendarUnit;
    notification.fireDate = [calendar dateFromComponents:componentsForFireDate];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

Hope It will work for you.

cyberlobe
  • 1,783
  • 1
  • 18
  • 30
0

Swift 3, sample of local notification for a specific day

let note = UILocalNotification()
note.alertBody = "text goes here"
note.soundName = UILocalNotificationDefaultSoundName                

let calendar = Calendar.autoupdatingCurrent
var comps = DateComponents()
comps.hour = 8
comps.minute = 15
comps.second = 0
comps.day = day

let date = calendar.date(from: comps)
note.fireDate = date
note.repeatInterval = .weekOfYear

UIApplication.shared.scheduleLocalNotification(note)
Benny Davidovitz
  • 1,152
  • 15
  • 18
0

The quick answer in Swift. 'UILocalNotification' was deprecated in iOS 10.0. So UNNotificationRequest will be recommended.

let content = UNMutableNotificationContent()
content.title = "YOUR TITLE"
content.subtitle = "YOUR SUBTITLE"
content.body = "YOUR BODY"
content.badge = 1
content.sound = UNNotificationSound.default()

var date = DateComponents()
date.weekday = 0 ,6 // which is Sunday and Friday
date.hour = 17
date.minute = 46

let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "notificationName", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: {error in
            print("success")
        })
    }
// add these lines to check if you added the notification successfully
UNUserNotificationCenter.current().getPendingNotificationRequests(
    completionHandler: { (notficiations) in
        for localNotification in notficiations {
            print(localNotification)
        }
    })
Allen
  • 2,979
  • 1
  • 29
  • 34