0

我为我的应用程序上的每日本地通知编写了一个代码,该代码将在每天下午 1:00(13:00)发送。我写它的那天,代码运行良好,并且通知在下午 1:00 准确发送。由于未知原因,它仅比现在效果好,但现在不起作用,我没有收到任何通知。

(我在应用程序的注册页面中编写了代码)

编码:

import UIKit
import FirebaseAuth
import Firebase

class SignUp: UIViewController {

    @IBOutlet weak var emailSignupTF: UITextField!
    @IBOutlet weak var passwordSignupTF: UITextField!
    @IBOutlet weak var errorLabel: UILabel!
    var message = ""

    override func viewDidLoad() {
        super.viewDidLoad()        
        navigationItem.setHidesBackButton(true, animated: true)

        //First Notification//
        let content = UNMutableNotificationContent()
        content.title = "תזכורת"
        content.body = "לא לשכוח לעדכן את מיקומך בתדריך הקרוב"

        // Configure the recurring date.
        var dateComponents = DateComponents()
        dateComponents.calendar = Calendar.current
        dateComponents.hour = 13
        dateComponents.minute = 0


        // Create the trigger as a repeating event.
        let trigger = UNCalendarNotificationTrigger(
                 dateMatching: dateComponents, repeats: true)

        // Create the request
        let uuidString = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuidString,
                    content: content, trigger: trigger)

        // Schedule the request with the system.
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
           if error != nil {
              // Handle any errors.
           }
        }
}
4

1 回答 1

-1

这里的问题是您通过说dateComponents.calendar = Calendar.current然后设置小时和分钟值来专门获取今天的日期。这意味着通知将在每年的该日期在指定的小时和分钟发送。要解决此问题,只需执行以下操作:

// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.hour = 13
dateComponents.minute = 0

现在通知将在每天 13:00 发送。参考:https ://developer.apple.com/documentation/usernotifications/uncalendarnotificationtrigger

于 2020-03-13T16:58:40.510 回答