didReceiveRemoteNotification 在单击通知(背景)时不工作,但在应用程序为前台时工作
Posted
技术标签:
【中文标题】didReceiveRemoteNotification 在单击通知(背景)时不工作,但在应用程序为前台时工作【英文标题】:didReceiveRemoteNotification Not Working when clicked on notification (background) but when working when app is Foreground 【发布时间】:2020-02-14 10:28:49 【问题描述】:需要一些关于推送通知的解决方案。我从堆栈中尝试了不同的解决方案,但对我不起作用!
我的问题是,当我从 PHP 脚本 触发通知时,通知本身会被触发。单击我想从我的应用程序导航到新视图的通知的事情。
当应用程序处于后台模式并且点击通知时不会出现完成处理程序警告,但是当应用程序处于活动状态 并触发通知完成工作并导航到另一个视图 请回复此问题,我已附上以下代码。
在后台模式下点击通知时出错 警告:UNUserNotificationCenter 委托收到了对 -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: 的调用,但从未调用过完成处理程序。
感谢任何帮助
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
// If you are receiving a notification message while your app is in the background,
let application = UIApplication.shared
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "LaunchScreenViewController")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
if let messageID = userInfo[gcmMessageIDKey]
print("Message ID: \(messageID)")
// Print full message.
print(userInfo)
print("Hello u have entered through Push notification ")
completionHandler(UIBackgroundFetchResult.newData)
【问题讨论】:
你试过用didReceiveNotificationResponse
代替didReceiveRemoteNotification
吗?
您好,看来您已经实现了 UNUserNotificationCenterDelegate 并且调用了不同的方法来处理通知。因为你的日志调用的是userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
方法而不是application(_:didReceiveRemoteNotification:fetchCompletionHandler:)
这能回答你的问题吗? didReceiveRemoteNotification not working in the background
当您使用 UNUserNotificationCenter 时,您不需要实现didReceiveRemoteNotification
方法。您需要实现UNUserNotificationCenterDelegate 那里有将被调用来处理通知的方法。在这里,您还有关于带有代码 sn-ps 的推送通知的 Apple 文章:developer.apple.com/documentation/usernotifications/…
【参考方案1】:
您好,我刚刚得到了答案! 我尝试在 usernotificationcenter 扩展方法 中导航到另一个视图,并且成功了。
extension AppDelegate: UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
print("Entry through the Notification")
let application = UIApplication.shared
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "stationListVC")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
if response.actionIdentifier == "remindLater"
let newDate = Date(timeInterval: 900, since: Date())
scheduleNotification(at: newDate)
感谢大家的帮助;)
【讨论】:
以上是关于didReceiveRemoteNotification 在单击通知(背景)时不工作,但在应用程序为前台时工作的主要内容,如果未能解决你的问题,请参考以下文章
尽管 content_available = true,但没有在后台调用 didReceiveRemoteNotification