3

I am trying to send a notification on a button click. This is the code in my viewController:

@IBAction func getNotificationButtonPressed(_ sender: Any) {
    let content = UNMutableNotificationContent()
    content.title = "Title"
    content.body = "Body"
    content.categoryIdentifier = "ident"
    content.sound = UNNotificationSound.default()

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)

    let request = UNNotificationRequest(identifier: "ident", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()

    center.add(request) { (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        } else {
            print ("success")
        }
    }
}

also in AppDelegate I have requested permission to use Notifications:

let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }
    application.registerForRemoteNotifications()

P.S. I have imported

import UserNotifications

in both AppDelegate and the custom ViewController.

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
Sardzoski
  • 33
  • 5
  • The way you have setup your notification it would fire right away. You don't even have time to close your app. How do you expect to receive a notification if your app it is not even in the background? – Leo Dabus Apr 20 '17 at 12:04
  • https://useyourloaf.com/blog/local-notifications-with-ios-10/ Refer this all step its working fine. – Himanshu Moradiya Apr 20 '17 at 12:06
  • import UserNotifications import this and declare this delegates UNUserNotificationCenterDelegate and add this in didFinishLaunchingWithOptions method let center = UNUserNotificationCenter.current() center.delegate = self return true – Himanshu Moradiya Apr 20 '17 at 12:09
  • also check that you are authorised for rise notification. dont forgot to check this . – Himanshu Moradiya Apr 20 '17 at 12:16

1 Answers1

2

When you create notification at that check

 if #available(iOS 10.0, *) {
     let content = UNMutableNotificationContent()
     content.title = "Intro to Notifications"
     content.subtitle = "Lets code,Talk is cheap"
     content.body = "Sample code from WWDC"
     content.sound = UNNotificationSound.default()
            // Deliver the notification in five seconds.
     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
     let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

     UNUserNotificationCenter.current().delegate = self
     UNUserNotificationCenter.current().add(request){(error) in

           if (error != nil){

                    print(error?.localizedDescription)
           }
       }
} 

Implement this delegates method UNUserNotification for Notification being triggered.

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    print("Tapped in notification")
}


//This is key callback to present notification while the app is in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    print("Notification being triggered")
    //You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
    //to distinguish between notifications
    if notification.request.identifier == requestIdentifier{

        completionHandler( [.alert,.sound,.badge])

    }
}

Happy coding.

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49