Skip to content

Instantly share code, notes, and snippets.

@JayachandraA
Created December 27, 2018 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JayachandraA/f066755aa48954cf3fc2e24b0add9967 to your computer and use it in GitHub Desktop.
Save JayachandraA/f066755aa48954cf3fc2e24b0add9967 to your computer and use it in GitHub Desktop.
The following snippet of code provides how to display a locations to the user. In order to display the notification first of all you are required to request the user permissions to send your local notification.
// request for user permisstions
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.badge, UNAuthorizationOptions.sound, UNAuthorizationOptions.alert]) { (yes, error) in
}
// fill up with the following objects
let context = UNMutableNotificationContent()
context.badge = NSNumber(integerLiteral: UIApplication.shared.applicationIconBadgeNumber + 1)
context.title = "It is Gym time now"
context.body = "Now you have to wake up and go to Gym."
context.launchImageName = "icon"
//UNTimeIntervalNotificationTrigger, UNCalendarNotificationTrigger, UNLocationNotificationTrigger
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let localNotification = UNNotificationRequest(identifier: "alarm_notificatio_id", content: context, trigger: trigger)
// finally add your notification to the center
UNUserNotificationCenter.current().add(localNotification) { (error) in
print(error as Any)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment