简体   繁体   中英

Sending local notifications in SWIFT

I added this code to my first ViewController

 // Step-1 Ask permission from User
    let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.badge,.sound,.alert]) { granted, error in
        if error == nil {
            print("User permission is granted : \(granted)")
    }
  }
//        Step-2 Create the notification content
        let content = UNMutableNotificationContent()
        content.title = "Hello"
        content.body = "Welcome"
   
    
//        Step-3 Create the notification trigger
        let date = Date().addingTimeInterval(1)
        let dateComponent = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: false)
    
    
    
//       Step-4 Create a request
        let uuid = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
        
    
//      Step-5 Register with Notification Center 
        center.add(request) { error in

but 5 seconds pass without any notification shown. What is wrong? Could somebody show me how to fix it??

The notification comes but when the app is foreground it won't show until you implement UNUserNotificationCenterDelegate method

UNUserNotificationCenter.current().delegate = self

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
     completionHandler([.sound,.alert])
}

Also set

 let date = Date().addingTimeInterval(5)

to give it some time until app hits so you can test it after clicking home button

import UIKit

class ViewController: UIViewController , UNUserNotificationCenterDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.badge,.sound,.alert]) { granted, error in
            if error == nil {
                print("User permission is granted : \(granted)")
            }
      }
    //        Step-2 Create the notification content
            let content = UNMutableNotificationContent()
            content.title = "Hello"
            content.body = "Welcome"
       
        
    //        Step-3 Create the notification trigger
            let date = Date().addingTimeInterval(5)
            let dateComponent = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: false)
        
        
        
    //       Step-4 Create a request
            let uuid = UUID().uuidString
            let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
            
        
    //      Step-5 Register with Notification Center
            center.add(request) { error in
        
        
            }
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
         completionHandler([.sound,.alert])
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM