Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用swift 3中的sinch未收到通知_Ios_Swift3_Sinch - Fatal编程技术网

Ios 使用swift 3中的sinch未收到通知

Ios 使用swift 3中的sinch未收到通知,ios,swift3,sinch,Ios,Swift3,Sinch,当应用程序在后台时,我正在尝试处理来电通知,但没有收到通知。我能够建立连接时,应用程序是在前台。从Android设备到iOS进行调用。我还在sinch仪表板上上传了voip服务证书和apns证书。我还启用了目标功能中后台模式下的IP语音和远程通知选项。这是我的appdelegate代码处理通知 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplica

当应用程序在后台时,我正在尝试处理来电通知,但没有收到通知。我能够建立连接时,应用程序是在前台。从Android设备到iOS进行调用。我还在sinch仪表板上上传了voip服务证书和apns证书。我还启用了目标功能中后台模式下的IP语音和远程通知选项。这是我的appdelegate代码处理通知

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    navController = self.window!.rootViewController as! UINavigationController?


    let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let homePageVc = mainStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
    let menuVC = mainStoryBoard.instantiateViewController(withIdentifier: "MenuViewController") as! MenuViewController

    let leftSideNav = UINavigationController(rootViewController: menuVC)
    let homePageNav = UINavigationController(rootViewController: homePageVc)

    centerDrawwerController = MMDrawerController(center: homePageNav, leftDrawerViewController: leftSideNav)

    centerDrawwerController?.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.panningCenterView

    self.navController?.navigationBar.barTintColor = UIColor(red:26/255.0, green:41/255.0, blue:55/255.0, alpha: 1.0)
    window?.rootViewController = centerDrawwerController
    window?.makeKeyAndVisible()

    // Register for Sinch Notifications (for calling)
    self.push = Sinch.managedPush(with: .production)
    self.push?.delegate = self;
    self.push?.setDesiredPushTypeAutomatically()
    NotificationCenter.default.addObserver(self, selector: #selector(initSinchClient), name: NSNotification.Name(rawValue: "sinchUserId"), object: nil)


    defaultObj.setValue("", forKey: "sinchRegistered")
    return true
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
    self.push?.application(application, didReceiveRemoteNotification: userInfo)
}

 func initSinchClient() {
    //Sinch
    if sinchClient == nil{
        let userId = defaultObj.object(forKey: "sinchUserId") as! String
        // iOS key
        sinchClient = Sinch.client(withApplicationKey: "xxxxxxx", applicationSecret: "xxxxxxxx", environmentHost: "clientapi.sinch.com", userId: userId)
        sinchClient.call().delegate = self
        //            self.push = Sinch.managedPush(with: .production)
        //            self.push?.delegate = self;
        //            self.push?.setDesiredPushTypeAutomatically()
        self.push?.setDesiredPushType(SINPushTypeVoIP)
        self.push?.registerUserNotificationSettings()

        sinchClient.enableManagedPushNotifications()
        sinchClient.setSupportCalling(true)
        sinchClient.delegate = self
        sinchClient.start()
        sinchClient.startListeningOnActiveConnection()   //For recieving incoming call
        callClient = sinchClient.call()
    }
}


 func managedPush(_ managedPush: SINManagedPush!, didReceiveIncomingPushWithPayload payload: [AnyHashable : Any]!, forType pushType: String!) {
    if sinchClient == nil {
        self.initSinchClient()

    }
    _ = sinchClient?.relayRemotePushNotification(payload)
}

  func handleRemoteNotification(userInfo: NSDictionary){
    if (sinchClient != nil){
        let userId = defaultObj.object(forKey: "sinchUserId") as! String
        if userId != ""{
            self.initSinchClient()
        }
    }
    self.sinchClient.relayRemotePushNotification(userInfo as! [AnyHashable : Any])
}

   private func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    self.push?.application(application, didReceiveRemoteNotification: userInfo)
}

//MARK:- Sinch Delegate
   func clientDidStart(_ client: SINClient!) {
    defaultObj.setValue("sinchRegistered", forKey: "sinchRegistered")
    print("Clien started")
  }

func clientDidFail(_ client: SINClient!, error: Error!) {

}

func clientDidStop(_ client: SINClient!) {

}

func client(_ client: SINClient!, logMessage message: String!, area: String!, severity: SINLogSeverity, timestamp: Date!) {

}

func client(_ client: SINCallClient!, localNotificationForIncomingCall call: SINCall!) -> SINLocalNotification! {
    let notification = SINLocalNotification()
    notification.alertAction = "Answer"
    notification.alertBody = "Incoming call from\(call.remoteUserId)"
    return notification
}

func client(_ client: SINCallClient!, didReceiveIncomingCall call: SINCall!) {
    print("Incoming call")
    _call = call
    _call.delegate = self;
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    theIncomingCallScreen = storyboard.instantiateViewController(withIdentifier: "incomingCall") as! IncomingCallViewController
    theIncomingCallScreen.delegate = self
    let fullName = call.remoteUserId
    let fullNameArr = fullName?.components(separatedBy: "|")
    let gId = fullNameArr?[0]
    let guardSharedObj = GuardManager.shared()
    let gArr = guardSharedObj.GuardManagerArr
    for item in gArr! {
        if item.guardId == gId{
            theIncomingCallScreen.guardName = item.guardName as NSString
        }
    }
    self.window?.rootViewController?.present(theIncomingCallScreen, animated: true, completion:nil)
}

func answer() {
    _call.answer()
    theIncomingCallScreen.dismiss(animated: false, completion: nil)
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    theNewCallScreen = storyboard.instantiateViewController(withIdentifier: "callScreen") as! CallScreenViewController
    theNewCallScreen.delegate = self
    theNewCallScreen.guardName = theIncomingCallScreen.guardName
    theNewCallScreen.status = "Connected"
    self.window?.rootViewController?.present(theNewCallScreen, animated: true, completion:nil)
}

func decline() {
    _call.hangup()
    theIncomingCallScreen.dismiss(animated: false, completion: nil)
}

func hangUp() {
    _call.hangup()
    theNewCallScreen.dismiss(animated: false, completion: nil)
}

func callDidProgress(_ call: SINCall!) {
    // Post notification
    let statusDataDict:[String: String] = ["status": "Connecting.."]
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KSTATUSNOTIFICATION"), object: nil, userInfo: statusDataDict)
}

func callDidEstablish(_ call: SINCall!) {
    // Post notification
    let statusDataDict:[String: String] = ["status": "Connected"]
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KSTATUSNOTIFICATION"), object: nil, userInfo: statusDataDict)
}

func callDidEnd(_ call: SINCall!) {
    // Post notification
    let statusDataDict:[String: String] = ["status": "Disconnected"]
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KSTATUSNOTIFICATION"), object: nil, userInfo: statusDataDict)
    theNewCallScreen.dismiss(animated: false, completion: nil)
    theIncomingCallScreen.dismiss(animated: false, completion: nil)
}

func applicationWillEnterForeground(_ application: UIApplication) {
 /////
}

func applicationDidBecomeActive(_ application: UIApplication) {
///// 
}

您只需要向sinch客户端发送设备令牌

self.sinchClient?.registerPushNotificationDeviceToken(UserDefaults.standard.value(forKey: "deviceTokenForSinch") as! Data, type: SINPushTypeRemote, apsEnvironment: SINAPSEnvironment.development)