截至目前,我获得推送通知并在前景和后台状态下显示横幅。但是虽然它在后台工作正常但是当我在前台时我无法触发一个动作来打开一个特定的视图控制器一旦我点击横幅(应该只在我点击横幅时打开)。我该怎么做我的代码如下:
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
print("Recived: \(userInfo)")
completionHandler(.newData)
if state == .background {
if mediaId != nil{
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mP3ViewController = storyboard.instantiateViewController(withIdentifier:"MP3ViewController") as! MP3ViewController
mP3ViewController.media_ID = mediaId
self.navigationController?.pushViewController(mP3ViewController, animated:true)
}
}
}else if state == .active {
//What sould i do here to get the click
print("Running on foreground")
UIApplication.shared.applicationIconBadgeNumber = 0
let count = 0
UserDefaults.standard.setValue(count, forKey: "PUSH_COUNT")
UserDefaults.standard.synchronize()
}
}
答案 0 :(得分:2)
试试这个
在UNUserNotificationCenterDelegate
班级
AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate {
}
在AppDelegate
class
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("didReceive response")
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent notification")
completionHandler([.badge, .alert, .sound])
}
上面的方法会在推送通知出现在前台时调用。
修改: - 强>
添加由交互式通知委托
调用的此方法func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, for notification: UILocalNotification, completionHandler: @escaping () -> Void) {
print("identifier = \(identifier)")
}
编辑2:
func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], withResponseInfo responseInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {
print("identifier = \(identifier)")
}