大佬教程收集整理的这篇文章主要介绍了ios – 仅在通知出现后触发(swift3),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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) }}
APP DELEAGATE
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) } }
如果您的iOS 10以下,您可以在UIAppplicatinoDelegate上使用application:didReceivelocalnotification:然后开始讲话.
实施的想法是检查通知上的一些值,以确保您触发的“语音通知”.
如果您使用的是iOS 10,则可以使用UNUserNotificationCenterDelegateExample here,您可以查看示例实现.
以上是大佬教程为你收集整理的ios – 仅在通知出现后触发(swift3)全部内容,希望文章能够帮你解决ios – 仅在通知出现后触发(swift3)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。