加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift3 – Local Notificaton无法在模拟器iOS10.0中运行

发布时间:2020-12-14 04:48:26 所属栏目:百科 来源:网络整理
导读:override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. askForPermission() }@IBAction func addLocalNotification(_ sender: AnyObject) { addLocalNotification() }func addLocalN
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.
    askForPermission()  
}

@IBAction func addLocalNotification(_ sender: AnyObject) {

    addLocalNotification()    
}

func addLocalNotification() {

    let content = UNMutableNotificationContent()
    content.title = "iOS10.0"
    content.body = "Hello Buddy"
    content.sound = UNNotificationSound.default()
    // Deliver the notification in five seconds.
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5,repeats: false)
    let request = UNNotificationRequest.init(identifier: "FiveSecond",content: content,trigger: trigger)
    // Schedule the notification.
    let center = UNUserNotificationCenter.current()
    center.add(request) { (error) in
         print(error)
    }
    print("should have been added")
}

func askForPermission() {

    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted,error) in

    }
}

解决方法

您必须实现UNUserNotificationCenter的委托,以告知系统您希望在应用程序运行时显示通知.请参见此处的示例: https://github.com/jerbeers/DemoLocalNotification

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读