在 iOS 10 Swift 3 中查看推送通知时遇到问题
Facing trouble with viewing push notification in iOS 10 Swift 3
我的应用有两种初始状态。
- 登录
- 登录后包含仪表板和其他内容。
我的应用在像这样从 Appdelegate 切换视图时工作正常
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let userDataExist = CommonUserFunction.isUserDataExist() as Bool
if userDataExist == true {
let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController
let navigationController = UINavigationController()
navigationController.viewControllers = [homeViewController!]
self.window!.rootViewController = navigationController
}
else{
let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController
let navigationController = UINavigationController()
navigationController.viewControllers = [loginController!]
self.window!.rootViewController = navigationController
}
self.window?.makeKeyAndVisible()
return true
}
我在处理推送通知时遇到了问题。我在 iOS 10 年通过这些方法
收到了推送通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
// custom code to handle push while app is in the foreground
print(notification.request.content.userInfo)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// custom code to handle push while app is in the foreground
print(response.notification.request.content.userInfo)
}
当我点击通知托盘中的通知时,这两种方法都会被触发,而应用程序是前台还是后台。点击通知后,我必须显示详细信息页面。我尝试像之前在 didFinishLaunchingWithOptions 方法
中那样设置详细信息页面
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushResponse = response.notification.request.content.userInfo as NSDictionary
let rtype = pushResponse.object(forKey: "rtype") as? String
let rid = pushResponse.object(forKey: "rid") as? String
if rtype != nil {
if rtype == "5" {
if rid != nil {
let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
noticeDetailsViewController!.id = rtype!
noticeDetailsViewController?.fromPush = true
let navigationController = UINavigationController()
navigationController.viewControllers = [noticeDetailsViewController!]
self.window!.rootViewController = navigationController
}
}
}
}
每次我做那个应用程序崩溃。如何克服这一点。请帮忙。提前致谢。
终于使用观察者设计模式解决了这个问题。
当应用程序在后台并点击通知托盘中的通知时,post 就这样从这里收到通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushResponse = response.notification.request.content.userInfo as NSDictionary
let rtype = pushResponse.object(forKey: "rtype") as? String
let rid = pushResponse.object(forKey: "rid") as? String
if rtype != nil {
if rtype == "5" {
if rid != nil {
NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
}
}
}
}
要接收此通知初始化下面的代码到您的 viewController
NotificationCenter.default.addObserver(self, selector: #selector(self.getPushResponse), name: NSNotification.Name(rawValue: "getPush"), object: nil)
从下面的代码中得到响应
func getPushResponse(_ notification: Notification){
if let info = notification.object as? NSDictionary {
let rid = info.object(forKey: "rid") as? String
let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
noticeDetailsViewController?.id = rid!
noticeDetailsViewController?.fromPush = true
self.navigationController?.pushViewController(noticeDetailsViewController!, animated: true)
}
}
不要忘记像那样从 viewController 中删除观察者
deinit {
NotificationCenter.default.removeObserver(self)
}
如果应用程序在后台没有实例并点击托盘中的通知,那么如何显示没有实例的通知。这可以像 AppDelegate
中那样处理
let prefs: UserDefaults = UserDefaults.standard
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpPush")
prefs.synchronize()
}
检查你的初始 viewController 这个密钥是否存在
let prefs:UserDefaults = UserDefaults.standard
if prefs.value(forKey: "startUpPush") != nil {
let pushResponse = prefs.object(forKey: "startUpPush") as! NSDictionary
NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
}
像那样显示 detailsController 后不要忘记删除此键
if fromPush == true {
let prefs: UserDefaults = UserDefaults.standard
if prefs.value(forKey: "startUpPush") != nil {
prefs.removeObject(forKey: "startUpPush")
prefs.synchronize()
}
}
我的应用有两种初始状态。
- 登录
- 登录后包含仪表板和其他内容。
我的应用在像这样从 Appdelegate 切换视图时工作正常
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let userDataExist = CommonUserFunction.isUserDataExist() as Bool
if userDataExist == true {
let homeViewController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "HomeView") as? HomeViewController
let navigationController = UINavigationController()
navigationController.viewControllers = [homeViewController!]
self.window!.rootViewController = navigationController
}
else{
let loginController = self.window?.rootViewController?.storyboard?.instantiateViewController(withIdentifier: "LoginView") as? LoginController
let navigationController = UINavigationController()
navigationController.viewControllers = [loginController!]
self.window!.rootViewController = navigationController
}
self.window?.makeKeyAndVisible()
return true
}
我在处理推送通知时遇到了问题。我在 iOS 10 年通过这些方法
收到了推送通知@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) {
print("Handle push from foreground")
// custom code to handle push while app is in the foreground
print(notification.request.content.userInfo)
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// custom code to handle push while app is in the foreground
print(response.notification.request.content.userInfo)
}
当我点击通知托盘中的通知时,这两种方法都会被触发,而应用程序是前台还是后台。点击通知后,我必须显示详细信息页面。我尝试像之前在 didFinishLaunchingWithOptions 方法
中那样设置详细信息页面@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushResponse = response.notification.request.content.userInfo as NSDictionary
let rtype = pushResponse.object(forKey: "rtype") as? String
let rid = pushResponse.object(forKey: "rid") as? String
if rtype != nil {
if rtype == "5" {
if rid != nil {
let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
noticeDetailsViewController!.id = rtype!
noticeDetailsViewController?.fromPush = true
let navigationController = UINavigationController()
navigationController.viewControllers = [noticeDetailsViewController!]
self.window!.rootViewController = navigationController
}
}
}
}
每次我做那个应用程序崩溃。如何克服这一点。请帮忙。提前致谢。
终于使用观察者设计模式解决了这个问题。
当应用程序在后台并点击通知托盘中的通知时,post 就这样从这里收到通知
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let pushResponse = response.notification.request.content.userInfo as NSDictionary
let rtype = pushResponse.object(forKey: "rtype") as? String
let rid = pushResponse.object(forKey: "rid") as? String
if rtype != nil {
if rtype == "5" {
if rid != nil {
NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
}
}
}
}
要接收此通知初始化下面的代码到您的 viewController
NotificationCenter.default.addObserver(self, selector: #selector(self.getPushResponse), name: NSNotification.Name(rawValue: "getPush"), object: nil)
从下面的代码中得到响应
func getPushResponse(_ notification: Notification){
if let info = notification.object as? NSDictionary {
let rid = info.object(forKey: "rid") as? String
let noticeDetailsViewController = self.storyboard?.instantiateViewController(withIdentifier: "NoticeDetailsView") as? NoticeDetailsController
noticeDetailsViewController?.id = rid!
noticeDetailsViewController?.fromPush = true
self.navigationController?.pushViewController(noticeDetailsViewController!, animated: true)
}
}
不要忘记像那样从 viewController 中删除观察者
deinit {
NotificationCenter.default.removeObserver(self)
}
如果应用程序在后台没有实例并点击托盘中的通知,那么如何显示没有实例的通知。这可以像 AppDelegate
中那样处理let prefs: UserDefaults = UserDefaults.standard
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
prefs.set(remoteNotification as! [AnyHashable: Any], forKey: "startUpPush")
prefs.synchronize()
}
检查你的初始 viewController 这个密钥是否存在
let prefs:UserDefaults = UserDefaults.standard
if prefs.value(forKey: "startUpPush") != nil {
let pushResponse = prefs.object(forKey: "startUpPush") as! NSDictionary
NotificationCenter.default.post(name: Notification.Name(rawValue: "getPush"), object: pushResponse)
}
像那样显示 detailsController 后不要忘记删除此键
if fromPush == true {
let prefs: UserDefaults = UserDefaults.standard
if prefs.value(forKey: "startUpPush") != nil {
prefs.removeObject(forKey: "startUpPush")
prefs.synchronize()
}
}