When the app is in background mode or phone is in sleep state and a VoIp push is received the below function in AppDelagte directs the user (to the UserTableViewController
in the app) and posts a notification.
A notification observer in viewDidLoad of the UserTableViewController
observes the notification and calls the func simulateMyIncomingCallFromNotification
.
I noticed that when I send a VoIP push the second time the func simulateMyIncomingCallFromNotification
is called twice and on the third time, thrice and so on. How can I avoid multiple calls ?
Other SO answers, advised to remove the Notification observer, which I am doing even before setting one, as you can seen in the below extension, but this doesn't seem to solve my problem.
How could I resolve this issue ?
In AppDelegate:
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
let storyboard = UIStoryboard(name: "User", bundle: nil)
VC = storyboard.instantiateViewController(withIdentifier: "UserTableViewController") as! UserTableViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = VC
self.window?.makeKeyAndVisible()
NotificationCenter.default.post(name: Notification.Name("didReceiveIncomingVoipPush"), object: nil, userInfo: payloadDict)
}
In UserTableViewController
extension NotificationCenter {
func setObserver(_ observer: AnyObject, selector: Selector, name: NSNotification.Name, object: AnyObject?) {
print("NotificationCenter.default before: \(NotificationCenter.default)")
NotificationCenter.default.removeObserver(observer, name: name, object: object)
NotificationCenter.default.addObserver(observer, selector: selector, name: name, object: object)
print("NotificationCenter.default after: \(NotificationCenter.default)")
}
}
fun viewDidLoad(){
NotificationCenter.default.setObserver(self, selector: #selector(self.simulateMyIncomingCallFromNotification(notification:)), name: Notification.Name("didReceiveIncomingVoipPush"), object: nil)
}