siiulan
siiulan

Reputation: 65

How to update table view when IOS app receives FCM notification

I am new to Swift and Firebase, I had trouble updating my table. My application has a table view on the main page. I expect the application would update the table every time I receive foreground and background FCM notifications.

In the foreground, how would I update the table view, what function or actions should I do to refresh or update the table view when the app receives an FCM notification in the foreground?

In the background, how would I also update the table view? In general, does the app refresh the page automatically or should I also update it by ourselves?

Can someone please help me with this? Thank you!

Upvotes: 1

Views: 195

Answers (1)

Satheesh
Satheesh

Reputation: 11276

You can expect the push notification to be delivered in the method of your AppDelegate. After which you can post a NSNotificationCenter broadcast.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  NotificationCenter.default.post(name: "AppNotification", object: "myObject", userInfo: userInfo)
}

which your view controller can observe and refresh your table view.

In your ViewController:

func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(appnotification), name: "AppNotification", object: nil)
}

@objc func appnotification(notification: Notification) {
    print(notification.userInfo ?? "")
    yourTableView.reloadData()
}

Upvotes: 1

Related Questions