Skip to content

Instantly share code, notes, and snippets.

@randhirraj3130
Created March 27, 2017 12:44
Show Gist options
  • Save randhirraj3130/73917c5b5d0805078232dbb60a8f056f to your computer and use it in GitHub Desktop.
Save randhirraj3130/73917c5b5d0805078232dbb60a8f056f to your computer and use it in GitHub Desktop.
how to apply local notification in swift 3
// add the following code in app delegate
import UserNotifications
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
} else {
// Fallback on earlier versions
}
application.registerForRemoteNotifications()
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void){
}
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
}
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
}
//apply the notification where you need
func userNotification(notification_title:String,notification_body:String){
print("userNotification working")
if #available(iOS 10.0, *) {
let content = UNMutableNotificationContent()
content.title = notification_title
content.sound = UNNotificationSound.default()
content.body = notification_body
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: "FiveSecond", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print("error \(error)")
}
print("should have been added")
} else {
print("this device does not support notification ")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment