アプリがバックグラウンドモードになっているとき、または電話機がスリープ状態になっていてVoIpプッシュが受信されたとき、AppDelagteの以下の機能が(ユーザはUserTableViewController
)通知をポストします。オブザーバが削除されても通知オブザーバが複数回呼び出される
UserTableViewController
のviewDidLoadの通知オブザーバが通知を参照し、func simulateMyIncomingCallFromNotification
を呼び出します。
私は、VoIPプッシュを送信すると、func simulateMyIncomingCallFromNotification
が2回、3回目に3回呼び出されることに気付きました。どのように複数の通話を避けることができますか?
他のSOの回答は、以下の拡張で見られるように、私が設定している前でも通知オブザーバを削除するように勧められていますが、これは私の問題を解決してくれないようです。
どうすればこの問題を解決できますか? 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)
}
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)
}
'メソッドにブレークポイントを追加するか、そのメソッドにdeinit()を追加するのですか? – user44776
複数回呼び出されるメソッドsimulateMyIncomingCallFromNotification – ankit