gpt4 book ai didi

ios - UNUserNotification 在 ios 中立即发送通知

转载 作者:行者123 更新时间:2023-11-30 12:48:55 26 4
gpt4 key购买 nike

我想在应用程序进入后台后,每隔 60 秒发送一次通知。但是当你进入后台时,通知会立即发送,之后每60秒发送一次通知。我不想立即发送通知,我该怎么做?

这是我的代码,

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Hello,", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "Some", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = NSNumber(value: UIApplication.shared.applicationIconBadgeNumber + 1)
content.categoryIdentifier = "com.mahmut.localNotification"

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 60.0, repeats: true)
let request = UNNotificationRequest.init(identifier: "60Sec", content: content, trigger: trigger)

let center = UNUserNotificationCenter.current()
center.add(request)

}

最佳答案

尝试以这种方式使用它...而不是 4,您可以使用自己的时间...稍后在您的函数中,您可以使用计时器概念每 60​​ 秒发送一次通知。

    let triggerTime = (Int64(NSEC_PER_SEC) * 4)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
self.callYourFunction()
})

关于ios - UNUserNotification 在 ios 中立即发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41256844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com