Получить уведомление от Синча, когда приложение убито - PullRequest
0 голосов
/ 25 октября 2018

У меня есть собственное приложение чата с функцией видео звонка с использованием библиотеки Sinch.Моя проблема в том, что когда я убиваю свое приложение, я не получаю никаких уведомлений, чтобы сообщить пользователю, что у него есть входящий звонок.Когда приложение в фоновом режиме, все работает отлично.Поэтому, пожалуйста, покажите мне, как я могу это исправить, я использую pushkit с сертификатом voip.

// delegate class sinch notification
    func managedPush(_ unused: SINManagedPush?, didReceiveIncomingPushWithPayload payload: [AnyHashable : Any]?, forType pushType: String?) {

        if let dictionary = payload!["sin"]{
            let result_data = (dictionary as AnyObject) as! NSString
            if let data = result_data.data(using: String.Encoding.utf8.rawValue) {
                do {
                    json_data = (try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String : Any])!
                } catch {
                    print("Something went wrong")
                }
            }
        }
        handleRemoteNotification(payload)
    }

    func handleRemoteNotification(_ userInfo: [AnyHashable : Any]?) {
        if client != nil {
            let userId = UserDefaults.standard.object(forKey: "userId") as? String
            if userId != nil {
                initSinchClient(userId: userId!)
            }
        }

        result = client?.relayRemotePushNotification(userInfo)
        print(json_data)
        if (result?.isCall())! && (result?.call().isCallCanceled)! {
            self.presentMissedCallNotification(withRemoteUserId: result?.call().remoteUserId)
        }
    }

    func client(_ client: SINCallClient?, didReceiveIncomingCall call: SINCall?) {
        //   Find MainViewController and present CallViewController from it.
        let currentUser:User? = {
            let realm = try! Realm()
            return realm.currentUser()
        }()
        let id = currentUser?.userDoctor?.userInfo?.id
        if let dict = result?.call().headers as? [String : String] {
            remoteId = dict["REMOTE_USER_ID"]
        }

        if let callId = remoteId {
            if id == Int(callId) {
                let getcallvideo_vc = UIStoryboard.init(name: "Chat", bundle: nil).instantiateViewController(withIdentifier: "getcallvideo_vc") as! GetCallViewController
                var top: UIViewController? = window?.rootViewController
                while ((top?.presentedViewController) != nil) {
                    top = top?.presentedViewController
                }
                getcallvideo_vc.segueSinCall = call
                top?.present(getcallvideo_vc, animated: true, completion: nil)
            }
        }
    }

    func client(_ client: SINCallClient!, localNotificationForIncomingCall call: SINCall!) -> SINLocalNotification! {
        let currentUser:User? = {
            let realm = try! Realm()
            return realm.currentUser()
        }()
        let id = currentUser?.userDoctor?.userInfo?.id
        if let dict = json_data["public_headers"] as? [String : String] {
            let remoteUserId = dict["REMOTE_USER_ID"]
            let remoteName = dict["USER_NAME_CALL"] as! String
            if id! == Int(remoteUserId!)! {
                let notification = SINLocalNotification()
                notification.alertBody = "Cuộc gọi đến từ \(remoteName)"
                notification.soundName = UILocalNotificationDefaultSoundName
                return notification
            } else {
                return nil
            }
        }
        return nil
    }

    func presentMissedCallNotification(withRemoteUserId remoteUserId: String!) {
        let application = UIApplication.shared
        if application.applicationState == .inactive {
            let note = UILocalNotification()
            note.alertBody = "Cuộc gọi nhỡ từ \(remoteUserId!)"
            note.soundName = UILocalNotificationDefaultSoundName
            application.presentLocalNotificationNow(note)
        }
    }
...