ios – 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)
    
    

}

}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img