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

链接到 API 的本地通知内容不会每天更改

如何解决链接到 API 的本地通知内容不会每天更改

我的应用中有一个本地通知,每天上午 10 点可靠地触发。但是,通知中的内容基本上是从 URL 中的一段简单文本中获取的,该 URL 也每天都在变化。问题是通知中的内容不是每天都随着 URL 变化,而是每天都保持不变。初始安装时的通知授权在 App Delegate File 中工作正常。我来自场景委托文件代码如下:

在场景委托中:

var randomreference = ""

func conversion (){
    if let url = URL(string: "https://beta.ourmanna.com/api/v1/get/?format=text") {
        do {
            let randomVerse = try String(contentsOf: url)
            print(randomVerse)
            self.randomreference = String(randomVerse)
        } catch {
            print("Contents Could not be loaded from API")
        }
    } else {
        print("The API URL did not work")
    }
}

func callNotification(){
    
    let center = UNUserNotificationCenter.current()
    
    let content = UNMutableNotificationContent()
    content.title = "Your Daily Verse"
    content.body = randomreference
    content.sound = .default
    
    var fireDate = DateComponents()
    fireDate.hour = 10
    fireDate.minute = 00
    
    let trigger = UNCalendarNotificationTrigger(dateMatching: fireDate,repeats: true)

    let request = UNNotificationRequest(identifier: "reminder",content: content,trigger: trigger)
    center.add(request) { (error) in
        if error != nil {
            print("Error = \(error?.localizedDescription ?? "error local notification")")
        }
    }
}

两个函数(CallNotification 和转换)在 Scene Delegate 的以下区域中被调用

func applicationWillTerminate(_ application:UIApplication){
        let viewControllerWithYourFunction = SceneDelegate()
        viewControllerWithYourFunction.callNotification()
    }

func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data,release shared resources,and store enough scene-specific state information
        // to restore the scene back to its current state.
        conversion()
        callNotification()
    }

我是否遗漏了 Scene Delegate 中的某些内容?初始通知始终工作正常。我的一些测试人员说,它每天提供新数据大约三天,然后它保持不变并且没有改变。是否与手机上的数据未接收到无法更新内容或其他内容有关?任何帮助将不胜感激,谢谢!

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