繁体   English   中英

在 SWIFT 发送本地通知

[英]Sending local notifications in SWIFT

我将此代码添加到我的第一个 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

但是 5 秒过去了,没有显示任何通知。 怎么了? 有人可以告诉我如何解决它吗?

通知来了但是当应用程序在前台时它不会显示直到你实现UNUserNotificationCenterDelegate方法

UNUserNotificationCenter.current().delegate = self

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

还设置

 let date = Date().addingTimeInterval(5)

给它一些时间直到应用程序点击,这样你就可以在点击主页按钮后对其进行测试

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])
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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