When I first run my app the simulator is able to display notifications whilst the app is in the foreground.
当我第一次运行我的应用程序时,模拟器能够在应用程序位于前台时显示通知。
When I relaunch the app didReceive notification:
(i.e. notification method from iOS 9) is called instead of willPresent notification
(i.e. notification method from iOS 10) and no notifications are displayed whilst the app is in the foreground. However, a notification is displayed if the app is in the background.
当我重新启动app didReceive通知时(即来自iOS 9的通知方法)被调用而不是willPresent通知(即来自iOS 10的通知方法),并且当应用程序在前台时不显示通知。但是,如果应用程序位于后台,则会显示通知。
Neither this question or this question provided a solution to this problem.
这个问题或这个问题都没有为这个问题提供解决方案。
I get authorisation for notifications using this code:
我使用此代码获得通知授权:
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]? = [:]) -> Bool {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .sound]) { (granted, error) in
UNUserNotificationCenter.current().delegate = self
}
}
return true
}
And I've implemented the willPresent notification method:
我已经实现了willPresent通知方法:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) completionHandler(UNNotificationPresentationOptions.alert)
}
Here's how I create the notification:
以下是我创建通知的方法:
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = message
content.categoryIdentifier = identifier
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: false)
let request = UNNotificationRequest(
identifier: "identifier",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(
request, withCompletionHandler: nil)
Problem in a nutshell: How do I ensure the app is able to display a notification when the app is in the foreground after the first time that the app is run?
问题简而言之:在第一次运行应用程序后,如何确保应用程序在应用程序处于前台时能够显示通知?
Thanks in advance.
提前致谢。
1 个解决方案
#1
1
Try setting the delegate outside of the requestAuthorization
completion block. It's probably happening later than you want.
尝试在requestAuthorization完成块之外设置委托。它可能比你想要的晚发生。
I added a sample project here: https://github.com/jerbeers/DemoLocalNotification
我在这里添加了一个示例项目:https://github.com/jerbeers/DemoLocalNotification
If you build and run, tap the button, you'll see the local notification 3 seconds later. If you stop, build and run again, I see the local notification even while the app is running.
如果您构建并运行,请点击按钮,3秒后您将看到本地通知。如果您停止,构建并再次运行,即使应用程序正在运行,我也会看到本地通知。
#1
1
Try setting the delegate outside of the requestAuthorization
completion block. It's probably happening later than you want.
尝试在requestAuthorization完成块之外设置委托。它可能比你想要的晚发生。
I added a sample project here: https://github.com/jerbeers/DemoLocalNotification
我在这里添加了一个示例项目:https://github.com/jerbeers/DemoLocalNotification
If you build and run, tap the button, you'll see the local notification 3 seconds later. If you stop, build and run again, I see the local notification even while the app is running.
如果您构建并运行,请点击按钮,3秒后您将看到本地通知。如果您停止,构建并再次运行,即使应用程序正在运行,我也会看到本地通知。