当应用程序处于后台时我会收到通知,但当应用程序处于前台时则不会收到通知。我完全按照这里所说的去做。我做错了什么?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
let userInfo = response.notification.request.content.userInfo
...
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "HomeVC") as! UITabBarController
otherVC.selectedIndex = 2
UIApplication.shared.keyWindow?.rootViewController = otherVC;
completionHandler([.banner, .badge, .sound])
}
// As we know, this method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void) {
{
// Play a sound to let the user know about the invitation.
completionHandler(.sound)
}