Apologies for the weird headline, can't think of a better way to describe it.
I'm building my app and have managed to get push notifications to work (Yay!).
If I send a push notification to the phone and tap the notification the function didReceiveRemoteNotification: in AppDelegate gets called and works as expected.
If I ignore the notification and load the app as per normally, the UITableView page doesn't refresh and I have to move off the view and then back to get viewDidAppear to refresh the view.
How do I get the app / view to refresh the page when the user navigates back to the app?
Have you tried to get the tableView to reloadData from applicationDidBecomeActive or post a notification to reload your tableview from this method like below?
func applicationDidBecomeActive(application: UIApplication) {
yourVC.tableView.reloadData()
}
Related
I am trying to use the new UserNotifications framework for local notifications. I redirect the user to a different viewcontroller when the notification is clicked/open. The notification is not handled properly by UNUserNotificationCenterDelegate didReceive method.
When the notification is in the foreground, and then it is clicked, it redirects to the right viewcontroller, however, when the app is closed and the notification is opened, the app is launched, however, the main viewcontroller is displayed instead.
Problem wasn't actually with the didReceive, but instead with the way the transitions were being made being viewcontrollers, due to methods returning before completion.
I was simulating a normal user flow programmatically. The problem with that is that tableview:didselectRowAt was returning before the performSegue method inside it was executed.
I ended up manually loading the VCs instead and explicitly calling UInavigationController:pushViewController(vc:animated:) to push them on the navigation stack.
I implemented push notifications into my app. The user gets a push notification from the server whenever there is a new message for him.
When the user clicks on the push notification, it opens the app. I want my app to reload a tableview when this happens, showing the user the recent newsfeed.
Is it possible in Swift 3?
As I understand, you are interesting in case of app that was opened on your recent newsfeed ViewController, then is closed to background by the user. Later on while receiving push, user clicks on it and opens the same newsfeed controller.
You have to subscribe to UIApplicationWillEnterForeground notification in your UIViewController like
NotificationCenter.default.addObserver(self, selector: #selector(yourMethod), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
and implement yourMethod like
func yourMethod() {
// send request to update newsfeed
// then update tableView
// tableView.reloadData()
}
Don't forget unsubscribe
deinit {
NotificationCenter.default.removeObserver(self)
}
It's the simplest solution and I described the simplest case. There are some more complicated cases I didn't describe, for example
The app is closed and you have to make hierarchy based on the push received in willFinishLaunchingWithOptions.
The app is opened on the other page, in this case you have to reconfigure viewControllers hierarchy to show required ViewController.
In this case you have to pass some data to required ViewController from the pressed Push Notification.
Hope it helps!
I am making an iOS application. Where I have made that I get notification when near certain places. And that works fine. But I then want it to navigate to a certain view when I press the notification.
I am thinking just like when I get a sms. I press that notification for the sms and it then navigate me to the correct sms conversation.
When application receives a push notification, a method in UIApplicationDelegate is called. The notification needs to be handled differently depending on what state your app is in when it’s received:
If app wasn’t running and the user launches it by tapping the push
notification, the push notification is passed to your app in the
launchOptions of
application(_:didFinishLaunchingWithOptions:)
If app was running and in the foreground, the push notification
will not be shown this function will call immediately
application(_:didReceiveRemoteNotification:)
If app was running or suspended in the background and the user brings
it to the foreground by tapping the push notification, this function
will be called.
application(_:didReceiveRemoteNotification:)
So you can change the root view controller on these functions like this :-
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
//Change your root view controller
}
This checks whether the value for UIApplicationLaunchOptionsRemoteNotificationKey exists in launchOptions. If it does, this will be the push notification payload you sent.
I wrote a simple class to navigate to any view controller in the view hierarchy from anywhere in one line of code by just passing the class type, so the code you'll write will be also decoupled from the view hierarchy itself, for instance:
Navigator.find(MyViewController.self)?.doSomethingSync()
Navigator.navigate(to: MyViewController.self)?.doSomethingSync()
..or you can execute methods asynchronously on the main thread also:
Navigator.navigate(to: MyViewController.self) { (MyViewControllerContainer, MyViewControllerInstance) in
MyViewControllerInstance?.doSomethingAsync()
}
Here the GitHub project link: https://github.com/oblq/Navigator
I have been writing an app that involves updating several values depending on the time the user is in - I update these values using viewDidAppear but unfortunately this function is not being called when the app is loaded again after being sent to the background.
Is there any way to prevent the app from being sent to the background? Or to force the app to open on a certain page after being opened from the background? Or is there a function that is like viewDidAppear, but is always whenever the app is loaded from the background?
Thanks!
Note:
I tried this:
func applicationDidBecomeActive() {
viewDidAppear(false)
}
inside the viewController class, but it didn't work.
You may be looking for applicationWillEnterForeground(application: UIApplication). This method has to be implemented in AppDelegate, not in a controller
I found lots of "reloadData" for TableView questions here, but my case is different.
I have a notification App, that uses UILocalNotification to fire reminders.
I need to refresh my tableview in real time after an alert fires, without the need of closing and opening the app to do so...
Example:
User is viewing the scheduled reminders (tableView), when suddenly one of the reminders fires. At this moment, the tableView will reload, but only if the user quits application or go back to another view and open the tableView again. That can't happen cause the application breaks if the user press the row in the tableView witch showed the current reminder (that's already completed and doesn't exists anymore).
I need to reload tableView in real time, without leaving the tableView Controller view. any idea on how to do that?
When a local notification for your app fires while the app is in the foreground, the system sends the application:didReceiveLocalNotification: message to your app delegate. From there, you should notify the view controller (via a direct reference and message send or possibly with a custom NSNotification) to do whatever it needs to do in this case.