如何安排本地通知在应用程序打开时触发一次,而不会在用户重新打开时重复? (Swift, XCode, IOS)
How do I schedule local notifications to fire once when the app opens without having repeats when the user reopens it? (Swift, XCode, IOS)
我是 Swift 和 XCode 编程的新手,我在调试代码时遇到了问题。该应用程序应该每天在同一时间向用户发送一次通知。到目前为止我已经设置了基础,但是在测试时,如果我清除应用程序并重新打开它,它会安排两个通知而不是一个。我需要一些魔术吗要解决这个问题?
目前,scheduleNotification
函数正在viewDidLoad
下调用,以便它可以在启动后立即安排一切。我试过在 UNUserNotificationCenter.current().add(request)
下调用函数。我认为在下面调用它是有意义的,因为它只提示用户一次,所以当用户授予通知权限时,它会立即激活通知,而不会冒重复的风险。但这根本没有安排任何通知。
我做了更多研究,也许我在错误的文件中编写了通知?这一切都在视图控制器中。
我和我爸爸谈过,他非常喜欢软件,他建议用 ID 跟踪通知,如果我不想让它们出现,就删除它们。但这听起来很复杂,我不想再头疼了哈哈
有什么想法吗?
(另外,忽略一些与计算器有关的调用。这与我的问题无关)
//
// ViewController.swift
//
import UIKit
class ViewController: UIViewController {
// Calculator Outlets
@IBOutlet weak var calculatorWorkings: UILabel!
@IBOutlet weak var calculatorResults: UILabel!
var workings:String = ""
override func viewDidLoad() {
super.viewDidLoad()
clearAll()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {_, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
print("Request authorized")
// Repeatedly call function so program schedules notifications in advance
for i in 1...10 {
self.scheduleNotification(day: i)
}
}
}
}
// Phrases
let phrasesArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
// Handle Notifications
func scheduleNotification(day: Int) {
var dateComponents = DateComponents()
dateComponents.hour = 21
dateComponents.minute = 09
dateComponents.day = day
let content = UNMutableNotificationContent()
// App name is not included in notification bar
content.title = "Jenna's Wisdom:"
content.body = phrasesArray[(Int.random(in: 0..<9))]
content.sound = .default
content.interruptionLevel = .timeSensitive
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Request to send notifications
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print(error)
} else {
print("Success")
}
}
}
}
假设 ViewController 是第一个和 rootViewcontroller,您可以在添加新通知之前删除所有本地通知。
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
我是 Swift 和 XCode 编程的新手,我在调试代码时遇到了问题。该应用程序应该每天在同一时间向用户发送一次通知。到目前为止我已经设置了基础,但是在测试时,如果我清除应用程序并重新打开它,它会安排两个通知而不是一个。我需要一些魔术吗要解决这个问题?
目前,scheduleNotification
函数正在viewDidLoad
下调用,以便它可以在启动后立即安排一切。我试过在 UNUserNotificationCenter.current().add(request)
下调用函数。我认为在下面调用它是有意义的,因为它只提示用户一次,所以当用户授予通知权限时,它会立即激活通知,而不会冒重复的风险。但这根本没有安排任何通知。
我做了更多研究,也许我在错误的文件中编写了通知?这一切都在视图控制器中。
我和我爸爸谈过,他非常喜欢软件,他建议用 ID 跟踪通知,如果我不想让它们出现,就删除它们。但这听起来很复杂,我不想再头疼了哈哈
有什么想法吗?
(另外,忽略一些与计算器有关的调用。这与我的问题无关)
//
// ViewController.swift
//
import UIKit
class ViewController: UIViewController {
// Calculator Outlets
@IBOutlet weak var calculatorWorkings: UILabel!
@IBOutlet weak var calculatorResults: UILabel!
var workings:String = ""
override func viewDidLoad() {
super.viewDidLoad()
clearAll()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {_, _ in
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
print("Request authorized")
// Repeatedly call function so program schedules notifications in advance
for i in 1...10 {
self.scheduleNotification(day: i)
}
}
}
}
// Phrases
let phrasesArray = ["0", "1", "2", "3", "4", "5", "6", "7", "8"]
// Handle Notifications
func scheduleNotification(day: Int) {
var dateComponents = DateComponents()
dateComponents.hour = 21
dateComponents.minute = 09
dateComponents.day = day
let content = UNMutableNotificationContent()
// App name is not included in notification bar
content.title = "Jenna's Wisdom:"
content.body = phrasesArray[(Int.random(in: 0..<9))]
content.sound = .default
content.interruptionLevel = .timeSensitive
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Request to send notifications
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print(error)
} else {
print("Success")
}
}
}
}
假设 ViewController 是第一个和 rootViewcontroller,您可以在添加新通知之前删除所有本地通知。
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()