ios - 仅在通知出现后触发操作(swift3)

标签 ios date swift3 notifications action

我的代码使用了一个 datePicker,当 datePicker 发生变化并匹配用户设备的当前日期时。它会触发一个通知。但是,我正在使用的 AVSpeechUtterance 会在 datePickers 时间更改后立即触发,而不是在通知出现时触发。我希望同时触发 AVSpeechUtterance 和通知。

import UIKit
import  AVFoundation
import UserNotifications

class ViewController: UIViewController {
@IBOutlet var datePicker: UIDatePicker!

@IBAction func datePicker(_ sender: Any) {
    let c = UNMutableNotificationContent()
    c.title = "Lets Roll"
    c.subtitle  = "s"
    c.body = "d"

    let begin = AVSpeechUtterance(string: " Hello ")

    let synthesizer = AVSpeechSynthesizer()

    begin.voice = AVSpeechSynthesisVoice(language: "en-US")
    begin.rate = 0.08

    synthesizer.speak(begin)

    let triggerDate = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: datePicker.date )
    let t = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: false)
    let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)

    UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
}}

应用委托(delegate)

  import AVFoundation
   import UIKit
   import UserNotifications

 enum NotificationName: String {
case mySpeechNotification
 }


   @UIApplicationMain
   class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if error != nil {
                print("Ops, error trying to get authorization")
            } else {
                if !granted {
                    print("Dude, let me use notifications!")
                }
            }
        }
    }
    return true
}

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)     {
     print("Oh, will present a notification, let's see the identifier: \(notification.request.identifier)")
    if (notification.request.identifier == NotificationName.mySpeechNotification.rawValue) {
        print("Speaking...")
               } else {
        print("Nothing to say...")
    }

    completionHandler(.alert)
    let begin = AVSpeechUtterance(string: " Hello ")
    begin.voice = AVSpeechSynthesisVoice(language: "en-US")
    begin.rate = 0.08

    let synthesizer = AVSpeechSynthesizer()
    synthesizer.speak(begin)


}

  }

error message

最佳答案

您可以在收到通知时执行语音操作。为此,您可以:

如果你低于 iOS 10,你可以在 UIAppplicatinoDelegate 上使用 application:didReceiveLocalNotification: 然后开始你的演讲。

实现的一个想法是检查通知上的某些值,以确保这是您触发的“语音通知”。

如果您使用的是 iOS 10+,则可以使用 UNUserNotificationCenterDelegateExample here您可以查看示例实现。

关于ios - 仅在通知出现后触发操作(swift3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45174129/

相关文章:

objective-c - "Current language: auto; currently objective-c"是什么意思?

ios - swift 3 WKWebView 'URLRequest';您的意思是使用 'as' 进行显式转换吗? ( 漏洞 )

.net - VB日期转换

javascript - 为什么 javascript 的 .getTime() + 24*60*60*1000 在 2013 年 10 月 27 日之后获取堆栈?

Python:如何从日期列表中计算日期范围?

authorization - 检查 Swift 3 中 MPMediaLibrary 的授权

ios - 将导航 Controller 添加到选项卡栏应用程序

ios - 具有自定义数据源的 UITableViewController 不显示单元格

ios - 使用 contains 检查 NSManagedObject Array 是否具有特定键的值

objective-c - 从 UITabBarController 中删除所有项目