微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐