微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS上的LocalNotifications无法正常工作

如何解决iOS上的LocalNotifications无法正常工作

我的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)

}

解决方法

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。