我刚刚对 iOs 10 和其他人的通知进行了更改:
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
let content = UNMutableNotificationContent()
content.body = notifMessage!
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "Upload", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request)
}
} else {
let notification = UILocalNotification()
notification.alertBody = notifMessage
notification.fireDate = NSDate() as Date
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}
当我通过将它与 USB 连接在我的设备上运行我的应用程序时,它可以工作,但只有当应用程序处于后台时,它才会在以下情况下工作:
我杀了应用程序
显示应用程序时