所以我一直在尝试向新的 UNUserNotificationCenter 添加通知,但我似乎没有得到它。
我的视图控制器有一个操作:
@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()
content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)
// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print(error)
}
print("should have been added")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
}
}
我有一个Notification Content Extension
在项目中也是如此,但它似乎根本没有被触发,我缺少什么想法吗?我正在尝试用户文档中的示例,但它并没有告诉我更多信息,或者我已经错过了它。
Here: https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent
Also:
https://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotificationsui
https://developer.apple.com/reference/usernotifications https://developer.apple.com/reference/usernotifications
Edit:
因此,将应用程序放在后台就达到了目的。
您需要注册通知...我尝试过,这有效。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
return true
}
Edit:从 iOS 10 开始,您无需将应用程序置于后台即可显示通知。
使用下面的回调将通知配置为在前台显示。
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
Here https://github.com/anishparajuli555/ios10LocalNotification是一个示例项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)