I am seeking some solutions regarding push notifications. I tried different solutions from Stack Overflow but couldn't get them to work.
My issue is that when I triggered the notification from my PHP script the notification itself get triggered . The thing on clicking the notification I want to navigate to a New View from my app .
When the app is in background mode and clicking on notification does nothing with a completion handler warning, but when the app is in the active state and triggering notification does the work and it navigates to the other view.
Error when notifications are clicked on background mode:
Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
Code:
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)
}
I just got a solution for my problem. I tried navigation to the other view in the usernotificationcenter extension method, and it worked:
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)
}
}
}