2

Need some solutions regarding the PUSH Notifications . I tried different solutions from the stack but couldn't worked for me !

My issue is that when I triggered the notfication from my PHP script the notification itself get triggerred . The thingon clicking the notification i want to navigate to a New View from my app .

When the app is in the 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 Please revert for this Issue i have attached the code below.

ERROR when notifications are clicked on background mode Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.

Any help is Appreciated

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)
    } 
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Deven Nazare
  • 538
  • 5
  • 24
  • 1
    did you try using `didReceiveNotificationResponse` instead of `didReceiveRemoteNotification`? – Abhishek Dave Feb 14 '20 at 10:47
  • Hello, it looks like you have implemented UNUserNotificationCenterDelegate and different method is called to handle notification. Because your log is calling `userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:` method instead of `application(_:didReceiveRemoteNotification:fetchCompletionHandler:)` – Łukasz Łabuński Feb 14 '20 at 10:50
  • Does this answer your question? [didReceiveRemoteNotification not working in the background](https://stackoverflow.com/questions/31450403/didreceiveremotenotification-not-working-in-the-background) – Mitesh jadav Feb 14 '20 at 11:00
  • 1
    When you are using UNUserNotificationCenter you don't need to implementing `didReceiveRemoteNotification` method. You need to implement [UNUserNotificationCenterDelegate](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate) there you have methods which will be called to handle notifications. Here you have also Apple article about Push Notifications with code snippets: https://developer.apple.com/documentation/usernotifications/handling_notifications_and_notification-related_actions – Łukasz Łabuński Feb 14 '20 at 11:03

1 Answers1

1

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)
        }
    }
} 
halfer
  • 19,824
  • 17
  • 99
  • 186
Deven Nazare
  • 538
  • 5
  • 24