本地通知未出现(Swift4) [英] local notification not appearing (Swift4)
本文介绍了本地通知未出现(Swift4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面的代码应该用作闹钟.当日期和时间匹配时,什么也没有发生,但是我在日志文件中看到了我的打印内容.请尝试此代码,并测试我做错了什么.似乎喜欢它曾经工作过.如果用户不使用该应用程序,则该消息在后台仍会消失.
My code below is supposed to act as an alarm clock. When the date and time matches nothing is happening but I am seeing my print in the log file. Please try this code and test what I am doing it wrong. It seemed liked it worked before. This is supposed to still go off in the background if the user is not using the app.
import UIKit;import AVFoundation;import UserNotifications
class ViewController: UIViewController, UNUserNotificationCenterDelegate {var timer = Timer();var isGrantedAccess = false;var player: AVAudioPlayer?
var passingDate : Date?
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()
@IBOutlet var taskIDo: UITextView!
override func viewWillAppear(_ animated: Bool)
{
createDatePicker()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(ViewController.testDate), userInfo: nil, repeats: true)
}
func playSound() {
let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}
func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
}
func testDate() {
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
{
return
}
passingDate = datePicker.date
if dptext.text != ""
{
let c = UNMutableNotificationContent()
c.title = "Lets Roll"
let t = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let r = UNNotificationRequest(identifier: "any", content: c, trigger: t)
UNUserNotificationCenter.current().add(r, withCompletionHandler: nil)
/////
let when = DispatchTime.now() + 20
DispatchQueue.main.asyncAfter(deadline: when) {
}
} else {
print("No its not empty")}}}
func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}}
推荐答案
几件事:
- 确保在ViewController中设置UNUserNotificationCenter.current().delegate.
- 如果您希望此内容在应用程序处于前台时显示,则这是不正确的.您需要最小化应用程序才能查看内部通知.
-
在您的应用程序委托中请求通知权限:
- Make sure you are setting the UNUserNotificationCenter.current().delegate in your ViewController.
- If you expect this to show when the app is in the foreground this isn't correct. You need to minimize the app in order to see the internal notification.
Request permissions for notifications in your app delegate:
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
这是我用来测试您的配置的代码:
Here's the code I used to test your configuration:
import UIKit
import AVFoundation
import UserNotifications
let center = UNUserNotificationCenter.current() // usernotification center
class ViewController: UIViewController, UNUserNotificationCenterDelegate {
var timer = Timer()
var isGrantedAccess = false
var player: AVAudioPlayer?
var passingDate : Date?
@IBOutlet var dptext: UITextField!
let datePicker = UIDatePicker()
@IBOutlet var taskIDo: UITextView!
override func viewWillAppear(_ animated: Bool) {
center.delegate = self
createDatePicker()
timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(testDate), userInfo: nil, repeats: true)
}
func playSound() {
let url = Bundle.main.url(forResource: "fc", withExtension: "mp3")!
do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }
player.prepareToPlay();player.play()} catch let error as NSError {print(error.description)}}
func createDatePicker() {
datePicker.datePickerMode = .dateAndTime
let toolbar = UIToolbar()
toolbar.sizeToFit()
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
toolbar.setItems([doneButton], animated: false)
dptext.inputAccessoryView = toolbar;dptext.inputView = datePicker
}
@objc func testDate() {
print("in testDate")
if Calendar.current.isDate(datePicker.date, equalTo: Date(), toGranularity: .minute) {
if let passingDate = passingDate, Calendar.current.isDate(datePicker.date, equalTo: passingDate, toGranularity: .minute)
{
print("in return")
return
}
passingDate = datePicker.date
let content = UNMutableNotificationContent()
content.badge = true
content.title = "Test"
content.body = "Lets Roll!"
content.sound = UNNotificationSound.default
let t = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let r = UNNotificationRequest(identifier: "any", content: content, trigger: t)
UNUserNotificationCenter.current().add(r, withCompletionHandler: { (error) in
dump(error)
})
}
}
@objc func donePressed() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .short
dptext.text = dateFormatter.string(from: datePicker.date)
self.view.endEditing(true)
}
}
这篇关于本地通知未出现(Swift4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文