单击通知(后台)时 didReceiveRemoteNotification 不工作,但在应用程序处于前台时工作
didReceiveRemoteNotification Not Working when clicked on notification (background) but when working when app is Foreground
需要一些关于推送通知的解决方案。我尝试了堆栈中的不同解决方案,但对我不起作用!
我的问题是,当我从 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)
}
您好,我刚刚找到了答案!!
我尝试在 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)
}
}
}
感谢大家的帮助 ;)
需要一些关于推送通知的解决方案。我尝试了堆栈中的不同解决方案,但对我不起作用!
我的问题是,当我从 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)
}
您好,我刚刚找到了答案!! 我尝试在 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)
}
}
}
感谢大家的帮助 ;)