当我的应用程序在远程通知进入时打开时,我真的需要帮助调用视图控制器方法,尤其是在使用TabBar导航时。当我在通知到达时打开我的应用程序时,它会调用 didReceiveRemoteNotification 因为我启用了“content-available = 1”。但问题是我不知道如何访问我的HomeViewController
方法refresh()
因为我使用TabBar Navigation.I需要调用{{在HomeViewController
内部的1}} refresh()
方法,因为它确实需要从核心刷新数据,并且到达的通知将保存在coredata中。
每次收到通知时,我都会将它们保存在领域数据库中,并在didReceiveRemoteNotification
显示用户
HomeViewController
这是来自func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
if let aps = userInfo["aps"] as? NSDictionary{
if let alert = aps["alert"] as? NSDictionary{
if let mbody = alert["body"] as? String{
print("Message Body : \(body)")
body = mbody
}
if let mtitle = alert["title"] as? String{
print("Message Title : \(title)")
title = mtitle
}
}
}
let newNotification = NotificationList()
newNotification.title = title
newNotification.body = body
//Realm insert query
oneSignalHelper.insertOneSignalNotification(newNotification)
print("Remote Receive")
//This condition isn't working
if let viewController = self.window?.rootViewController as? HomeViewController {
print("Remote Receive Read")
//This method is HomeViewController method which I gonna use for refresh when I tapped notification.It read that are save in realm database and show results.
viewController.readNotificationsAndUpdateUI()
}
handler(UIBackgroundFetchResult.NewData)
}
readNotificationsAndUpdateUI()
HomeViewController
答案 0 :(得分:1)
我建议您使用NSNotificationCenter,只需在didReceiveRemoteNotification
内发布通知,如下所示:
NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)
在viewDidLoad()
的HomeViewController中,您应该像这样订阅通知中心:
NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)
答案 1 :(得分:0)
不确定是否有帮助,但我根据先前的答案以这种方式解决了
加载情节提要
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
加载ViewController(检查故事板中是否有此ViewController的标识符!) 让bestellvorschlag = storyBoard.instantiateViewController(identifier:“ Bestellvorschlag”)为! Bestellvorschlag
在ViewController上加载函数
bestellvorschlag.loaddata() //Loads CoreData into Array of artikels
也许用结果数修改Tabbar徽章
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
appDelegate中的完整功能
func applicationDidBecomeActive(_ application: UIApplication) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let bestellvorschlag = storyBoard.instantiateViewController(identifier: "Bestellvorschlag") as! Bestellvorschlag
bestellvorschlag.loaddata()
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
}