本地推送通知点击后如何重定向?

问题描述 投票:0回答:1

我构建了一个星座应用程序,它有一个本地推送通知。我想在单击通知时将其重定向到特定页面,但没有。我的应用程序首页始终打开。

我的应用程序委托文件;

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()
    }
}

我的本地推送功能;

    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)   
    } 
}
ios swift apple-push-notifications uilocalnotification
1个回答
0
投票

我认为您缺少 UNUserNotificationCenter

delegate
。默认情况下,如果您注册了通知服务,则会在
didReceiveRemoteNotification
中的
UIApplicationDelegate
中收到。但是,您正在实现
UNUserNotificationCenterDelegate
而不分配任何委托。

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self //Where self is your AppDelegate
...
© www.soinside.com 2019 - 2024. All rights reserved.