说明为什么用户在Swift中请求许可之前应该打开推送通知

时间:2019-06-29 04:16:56

标签: swift push-notification apple-push-notifications

我想发送一个UIAlert。完成后,它应询问用户是否有权发送通知。我基本上是从Stack Overflow逐字复制的,在解释之前,它一直在发送请求。

    func ask() -> Void {
    UNUserNotificationCenter.current() // 1
        .requestAuthorization(options: [.alert]) { // 2
            granted, error in
            print("Permission granted: \(granted)") // 3
    }
}

func addNotifacations() -> Void {
    let alert = UIAlertController(title: "Push notifacations", message: "LSToday can send you push notifications with important info about LS (Like snow days, schedule changes, and events around LS.) We will never spam you, and you can disable notifacations at any time in the iOS settings app.", preferredStyle: UIAlertController.Style.alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: { action in
        self.ask()
        self.impactFeedbackgeneratorM.impactOccurred()
        alert.dismiss(animated: true, completion: nil)

    }))
    impactFeedbackgeneratorH.prepare()
    self.present(alert, animated: true, completion: nil)

    days.set("completed", forKey: "firstLaunch")
    }

0 个答案:

没有答案