首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用SWIFT发送本地通知

用SWIFT发送本地通知
EN

Stack Overflow用户
提问于 2022-03-03 11:55:57
回答 1查看 701关注 0票数 0

我将这段代码添加到我的第一个ViewController中

代码语言:javascript
复制
 // 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秒过去了,没有显示任何通知。怎么啦?有人能告诉我怎么修吗??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-03 11:57:15

通知来了,但是当应用程序是前台时,只有实现UNUserNotificationCenterDelegate方法才会显示

代码语言:javascript
复制
UNUserNotificationCenter.current().delegate = self

代码语言:javascript
复制
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
     completionHandler([.sound,.alert])
}

也定

代码语言:javascript
复制
 let date = Date().addingTimeInterval(5)

给它一些时间,直到应用程序命中,所以您可以在单击home按钮后对其进行测试。

代码语言:javascript
复制
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])
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71336800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档