【发布时间】:2017-11-17 10:37:02
【问题描述】:
到目前为止,当我处于前台和后台状态时,我都会收到推送通知并显示横幅。但是,尽管它在后台运行良好,但当我在前台时,一旦我单击横幅,我就无法触发打开特定视图控制器的操作(仅当我单击横幅时才应打开)。我该怎么做。我的代码如下
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()
}
}
【问题讨论】:
-
如果应用程序打开,推送通知横幅将不会显示。因为您需要使用自定义横幅,当应用程序在 forgrond 时显示推送通知,您可以获得它的点击事件
-
在前台收到通知时,您必须从导航控制器中关闭所有视图控制器,然后尝试将您的愿望推送到一个控制器@danu
-
上面的代码可以很好地显示横幅,无需创建自定义。但是有触发点击事件的麻烦
-
是的,正如@NitinGohel 提到的,如果您的应用处于前台,您将不会收到横幅通知,但您会收到 didreceiveremotnotification 的回调
-
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { completionHandler([.badge, .alert, .sound]) // 这个委托将给我的横幅 }
标签: ios swift swift3 push-notification