iOS上的LocalNotifications无法正常工作

时间:2020-09-01 15:01:10

标签: ios swift uilocalnotification

我的iOS应用上的LocalNotifications有问题。

在我的应用程序上,用户可以安排通知以在合同到期时获得提醒。他可以自己选择日期和时间。 但是,我反复收到用户的电子邮件,说他们的通知不起作用。 但是,当在我自己的个人设备上进行测试时,我没有任何问题,它们可以正常工作。

我想知道我是否缺少对LocalNotifications的理解。他们会在某个时候到期吗?设备关闭时,UNUserNotificationCenter是否删除排队的通知?有这种行为的任何可能原因,还是其他人遇到过这种行为?

任何帮助将不胜感激。

我在下面包括了用于创建LocalNotification的代码:

/**
 Schedules a local notification.
 - parameter company: String! of the contracts company name.
 - parameter name: String! of the contracts name.
 - parameter datum: Date! of the reminder.
 
 - returns: String of NotificationID.
 */
static func scheduleNotification(company: String!, name: String!, datum: Date!) -> String! {
    let notificationID = arc4random()
    let content = UNMutableNotificationContent()
    content.title = NSLocalizedString("Kündigungserinnerung ", comment: "") + company
    content.body = NSLocalizedString("Falls Sie Ihren ", comment: "") + name + NSLocalizedString(" Vertrag kündigen möchten, wäre jetzt der richtige Zeitpunkt dafür! ?", comment: "")
    content.categoryIdentifier = Notification.Category.Local.identifier
    content.userInfo = ["NotificationID": notificationID]
    content.sound = UNNotificationSound.default()

    let calendar = Calendar.current
    let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: datum)

    // UnMark to check if Notifications work
    //let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    let finalComponents = NSDateComponents()
    finalComponents.year = components.year!
    finalComponents.month = components.month!
    finalComponents.day = components.day!
    finalComponents.hour = components.hour!
    finalComponents.minute = components.minute!

    let trigger = UNCalendarNotificationTrigger(dateMatching: finalComponents as DateComponents, repeats: false)
    let requestIdentifier = "request"
    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
        if error != nil {
            log.error(error!)
        }
    })
    return String(notificationID)

}

1 个答案:

答案 0 :(得分:0)

来自另一个论坛的答案解决了我的问题:

https://developer.apple.com/forums/thread/658282