为什么我的本地通知在 iOS 10 中没有在前台触发?
Why is my local notifications not triggered in the foreground in iOS 10?
我想了解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码以允许此功能,但是当我的应用程序在前台时没有显示横幅。
这是我在 AppDelegate.swift
中添加的内容:
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in
if willAllow == true
{
// Do something
}
}
UNUserNotificationCenter.current().delegate = self
// Override point for customization after application launch.
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("GO IN HERE")
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
}
}
我的ViewController.swift
:
override func viewDidLoad()
{
super.viewDidLoad()
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()
content.subtitle = "This is a test"
let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
当我的应用程序首次启动时,我确实看到允许我的应用程序推送通知的权限警报,但我根本没有看到任何通知。
我确实看到了日志输出 GO IN HERE,所以正在调用 willPresent
。
还有什么我想念的吗?
您永远不会在前台看到通知,这就是它们的工作方式。如果你想强制显示它们,你可以创建自己的 UIView 并显示它或使用现有的库 https://github.com/pikacode/EBForeNotification
参考以下回答:
我的本地通知没有在前台显示的原因不是因为我“永远不会在前台看到通知”,如前一个答案所述,而是因为我需要至少设置 body
内容,但我没有在我的代码中设置它。
通过设置 content.body = "Some message"
,我终于可以在我的应用处于 运行 时在前台看到通知。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
if !accepted {
print("Notification access denied")
}
}
}
在 AppDelegate 中添加:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler( [.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
对我来说,是我忘了像这样设置委托:
UNUserNotificationCenter.current().delegate = self
我想了解为什么本地通知没有显示在前台。我相信我添加了所有正确的代码以允许此功能,但是当我的应用程序在前台时没有显示横幅。
这是我在 AppDelegate.swift
中添加的内容:
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in
if willAllow == true
{
// Do something
}
}
UNUserNotificationCenter.current().delegate = self
// Override point for customization after application launch.
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("GO IN HERE")
completionHandler(.alert)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
}
}
我的ViewController.swift
:
override func viewDidLoad()
{
super.viewDidLoad()
let content: UNMutableNotificationContent = UNMutableNotificationContent()
content.sound = UNNotificationSound.default()
content.subtitle = "This is a test"
let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
当我的应用程序首次启动时,我确实看到允许我的应用程序推送通知的权限警报,但我根本没有看到任何通知。
我确实看到了日志输出 GO IN HERE,所以正在调用 willPresent
。
还有什么我想念的吗?
您永远不会在前台看到通知,这就是它们的工作方式。如果你想强制显示它们,你可以创建自己的 UIView 并显示它或使用现有的库 https://github.com/pikacode/EBForeNotification
参考以下回答:
我的本地通知没有在前台显示的原因不是因为我“永远不会在前台看到通知”,如前一个答案所述,而是因为我需要至少设置 body
内容,但我没有在我的代码中设置它。
通过设置 content.body = "Some message"
,我终于可以在我的应用处于 运行 时在前台看到通知。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
if !accepted {
print("Notification access denied")
}
}
}
在 AppDelegate 中添加:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler( [.alert, .badge, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
对我来说,是我忘了像这样设置委托:
UNUserNotificationCenter.current().delegate = self