yusuf kutlay
yusuf kutlay

Reputation: 19

How to redirect when local push notification clicked?

I build a horoscope app, its has a local push notification. I want to when clicked notification, redirect it to a specific page but don't. My app first page always opens.

my appDelegate File;

extension AppDelegate: UNUserNotificationCenterDelegate{
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        
        if response.notification.request.identifier == "asd"{
      
            if let tappadView = storyBoard.instantiateViewController(withIdentifier: "deneme") as? detailView{
                
                self.window?.rootViewController = tappadView
            }
            
            
        }
        
        completionHandler()
    }
}

my local push functions;

    func chechPermission(){
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.getNotificationSettings { settings in
            switch settings.authorizationStatus{
            case .authorized:
                self.dispatchNotification()
            case .denied:
                return
            case .notDetermined:
                notificationCenter.requestAuthorization(options: [.alert, .sound]) { didAllow, error in
                    if didAllow{
                        self.dispatchNotification()
                    }
                }
            default:
                return
            }
        }
        
    }
    
    
    func dispatchNotification(){
        let iddentifier =  "asd"
        let userNotification = UNUserNotificationCenter.current()
        let content = UNMutableNotificationContent()
        content.title = "Hu huu"
        content.body = "Yeni Geldi Yorumlar..."
        content.sound = .default
        let hour = 22
        let minute = 29
        let isDaily = true
        
   
        let calender = Calendar.current
        var dateComponent = DateComponents(calendar: calender, timeZone: TimeZone.current)
        dateComponent.hour = hour
        dateComponent.minute = minute
        
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: isDaily)
        let request = UNNotificationRequest(identifier: iddentifier, content: content, trigger: trigger)
        
        userNotification.removePendingNotificationRequests(withIdentifiers: [iddentifier])
        userNotification.add(request)   
    } 
}

Upvotes: 1

Views: 277

Answers (1)

yusuf kutlay
yusuf kutlay

Reputation: 19

I solve this code.

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  
    
    if response.notification.request.identifier == "deneme"  {
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
           let window = windowScene.windows.first {
            
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier: "burcListView") as! ViewController
            viewController.choosenTime = "0"
            window.rootViewController = viewController
        }
        
        }
        
    completionHandler()
    }

Upvotes: 0

Related Questions