【发布时间】:2018-09-22 06:28:46
【问题描述】:
我有一个数据库,其中存储有过期日期的产品。当到期日期到期时,用户应该会收到有关此的通知。但是如果用户关闭了他们的应用程序(杀死应用程序),这些通知也应该出现。问题:如何做到这一点?请给我看代码。 我的代码是(AppDelegate):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in }
return true
}
和(视图控制器):
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "\(overdueProductsStringArray)"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "t", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
【问题讨论】:
-
@Vipul 谢谢,但您的代码是 iOS
-
@Vipul 谢谢你对我的帮助!但是当它在前台应用时,它会为应用程序编码
标签: ios swift uilocalnotification