kiran
kiran

Reputation: 4409

Local Notification on weekly basis on button click

How to set notification on weekly basis on button action.

// Notification on weekly basis

-(IBAction)buttonAction:(id)sender{
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate =  [NSDate dateByAddingTimeInterval:60*60*24*7];
    localNotification.alertBody = @"Hello Message";
    localNotification.alertAction = @"Show me the item";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    localNotification.repeatInterval = NSWeekCalendarUnit;
}

on every week application will notify the message "Hello Message"

Upvotes: 1

Views: 452

Answers (2)

nmh
nmh

Reputation: 2503

  • Now is 2:00PM Sat Nov 5
  • I have a picker is set to 2:10PM Sat Nov 5
  • A local notification will be displayed at 2:10PM Sat Nov 5
  • The notification will continue being notified at 2:10PM Sat Nov 12

        let ok = UNNotificationAction(identifier: "OKIdentifier", title: "Open", options: [.foreground])
        let cancel = UNNotificationAction(identifier: "CancelIdentifier", title: "Dismiss", options: [])
        let category = UNNotificationCategory(identifier: "message", actions: [ok, cancel], intentIdentifiers: [], options: [])
        UNUserNotificationCenter.current().setNotificationCategories([category])
    
        let content = UNMutableNotificationContent()
        content.body = "open the SAMPLE app"
        content.categoryIdentifier = "message"
    
        let alarmTime = datePicker.date
        let alarmTimeComponent = Calendar.current.dateComponents([.hour, .minute, .weekday], from: alarmTime)
        let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponent, repeats: true)
    
        let request = UNNotificationRequest(identifier:"five second", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request){(error) in
            if (error != nil){
                //handle here
                print("error : \(error)"  )
            }
        }
    

Upvotes: 0

Greg Robertson
Greg Robertson

Reputation: 2347

Swift 2.2:

add:

localNotification.repeatInterval = .Weekday

Swift 3.0:

    let content: UNMutableNotificationContent = UNMutableNotificationContent()

    content.title = "Title"

    content.subtitle = "Subtitle"

    content.body = "Hello Message"

    let calendar = Calendar.current

    let alarmTime = Date().addingTimeInterval(60*60*24*7)
    let alarmTimeComponents = calendar.dateComponents([.weekday], from: alarmTime)

    let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true)

    let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,
                                        content: content,
                                        trigger: trigger)

    UNUserNotificationCenter.current().add(request)
    {
        (error) in // ...
    }

Upvotes: 1

Related Questions