簡體   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