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)
}
}
sonle
10.4k3 gold badges19 silver badges33 bronze badges
1 Answer 1
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()
}
Sign up to request clarification or add additional context in comments.
1 Comment
Community
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Explore related questions
See similar questions with these tags.
lang-swift
didReceive response? Did it execute when you tapped on the notification?