Swift 3 - How to set up Local Notification at specific date

ios local notification repeat interval
open specific view when opening app from local notification swift
schedule local notification swift 4
ios local notification when app is in background
repeat local notification swift
uncalendarnotificationtrigger
local notification not working swift
swift local notification everyday

I need your help for a project. I have 3 variables, one for the day , another for the month and last for the year. Like that :

var year = 2017 var month = 06 var day = 19

I want to send a notification even if the app is close when we are at the date of these variable, but i'm not really good with Calendar and Date. I just made this app for the moment.

let myNotification = Notification.Name(rawValue:"MyNotification")

override func viewDidLoad() {
    super.viewDidLoad()

    let nc = NotificationCenter.default
    nc.addObserver(forName:myNotification, object:nil, queue:nil, using:catchNotification)
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    let nc = NotificationCenter.default
    nc.post(name:myNotification,
            object: nil,
            userInfo:["message":"Hello there!", "date":Date()])
}

func catchNotification(notification:Notification) -> Void {
    print("Catch notification")

    guard let userInfo = notification.userInfo,
        let message  = userInfo["message"] as? String,
        let date     = userInfo["date"]    as? Date else {
            print("No userInfo found in notification")
            return
    }

    let alert = UIAlertController(title: "Notification!",
                                  message:"\(message) received at \(date)",
        preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

Thank you in advance

You need to set up a local notification and use a UNCalendarNotificationTrigger to fire it at a specific date.

let dateComponents = DateComponents(year: year, month: month, day: day)
let yourFireDate = Calendar.current.date(from: dateComponents)
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey:
            "Your notification title", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Your notification body", arguments: nil)
content.categoryIdentifier = "Your notification category"
content.sound = UNNotificationSound.default()
content.badge = 1

let dateComponents = Calendar.current.dateComponents(Set(arrayLiteral: Calendar.Component.year, Calendar.Component.month, Calendar.Component.day), from: yourFireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: "Your notification identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if let error = error {
            //handle error
        } else {
            //notification set up successfully
        }
}

Launch a local notification at a specific time in iOS, Make the request early in your App life cycle such as in application:didFinishLaunchingWithOptions:. The first time your App requests authorization the system shows the user an alert, after that they can manage the permissions from settings: // Swift let options: UNAuthorizationOptions = [. alert, . The process for creating a local notification using Swift 3 for iOS 10, feels a lot simpler than previous versions. In this simple example, I barely touch on the capabilities of the new rich notifications you can create. I have created a GitHub repository with example code here.

In swift 3 - 4 use this.

  func scheduleLocal() {

        let dateComponents = DateComponents(year: 2019, month: 1, day: 17, hour: 10, minute: 20)
        let yourFireDate = Calendar.current.date(from: dateComponents)

        let notification = UILocalNotification()
        notification.fireDate = yourFireDate
        notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
        notification.alertAction = "be awesome!"
        notification.soundName = UILocalNotificationDefaultSoundName
        notification.userInfo = ["CustomField1": "w00t"]
        UIApplication.shared.scheduleLocalNotification(notification)


    }

Scheduling a Notification Locally from Your App, Create and schedule notifications from your app when you want to get the user's app could ask the system to display an alert when a particular task is finished. Always use local notifications to convey important information that the user wants. Configure the recurring date. var dateComponents = DateComponents()  If you have a device, or use the simulator, you can try this out by updating the setNotification function in ContentView.swift: Click on “Set Notification” and minimize the test app. After 5

Here is a way to add your Custom events to Calender.

let store = EKEventStore()
store.requestAccessToEntityType(.Event) {(granted, error) in
    if !granted { return }
    var event = EKEvent(eventStore: store)
    event.title = "Event"
    event.startDate = NSDate() //Event date
    event.endDate =    event.startDate.dateByAddingTimeInterval(60*60) //even duration ex.1hr
    event.calendar =     store.defaultCalendarForNewEvents
    do {
        try store.saveEvent(event, span: .ThisEvent, commit: true)
        self.savedEventId = event.eventIdentifier //save this id for future reference 
}     catch {
    // Display error 
    }
}

Scheduling notifications , So, in order to send local notifications in our app, we first need to request permission, This will configure all the data needed to schedule a notification, which is three For example, the trigger – when to show the notification – can be a calendar trigger that shows the notification at an exact time, it can be an 1, 2, 3, 4, 5. requestAuthorization (), the private function that will prompt the app user to give permission to send local notifications. scheduleNotifications (), the private function that will iterate over the notifications property to schedule the local notifications. This 3-tiered permissions check is common in iOS development.

Scheduling Local Notifications With Swift – LearnAppMaking, How do you send and receive local notifications in your iOS app? user permission to send local notifications; How to set up triggers for local notifications Check if the user has given permission to schedule/handle local notifications; If no This 3-tiered permissions check is common in iOS development. In this video, I am going to show you how to set up location notifications in Xcode 8, using Swift 3.0. Check out my Swift courses: https://www.udemy.com/use

Local Notifications with Swift 4 - Quick Code, By the end of this tutorial, this action will send a local notification with the contents of To create such a request, go to the AppDelegate class and import the A trigger for a specific date and time is created using the date components object. 3. Category registration in the notification center. It remains to  Send Push Notifications to iOS Devices using Xcode 8 and Swift 3 Oct 6, 2016. Push notifications are a great way to ensure your users re-engage with your app every once in a while, but implementing them on iOS can be challenging, especially with all of the changes in Xcode and Swift, not to mention the various iOS versions which deprecate the notification classes we grew accustomed to in the past.

How To Create iOS 10 Local Notifications Using Swift 3, How To Create iOS 10 Local Notifications Using Swift 3 Setup The Request For Permission To Use Notifications. As part of Time to break this code down! Local Notifications with Swift 4. Sending a calendar based notification. A trigger for a specific date and time is created using the date components object. use the correct set of date

Comments
  • what is the value of "yourFireDate" ?
  • it is a Date object you initialize using your variables year, month and day.
  • so i set let yourFireDate = year + month + day
  • No, how could you initialize a Date object by simply adding 3 integers? You have to use DateComponents, see my updated answer.
  • I have add a hour and a minute and ITS WORKS thank you ^^
  • Where did you see any reference to calendar events in the question? The question was clearly about sending a local notification at a specific date and it has nothing to do with setting up a calendar event.
  • He has tagged a calender tag in his question.