2017-09-12 10 views
1

앱이 백그라운드 상태이지만 수신 알림을받지 못하면 수신 전화 알림을 처리하려고합니다. 앱이 포 그라운드에있을 때 연결을 설정할 수 있습니다. Android 기기에서 iOS로 통화가 이루어집니다. 또한 sinch 대시 보드에 voip 서비스 인증서 및 apns 인증서를 업로드했습니다. 나는 또한 IP & 대상의 기능에서 배경 모드의 원격 알림 옵션을 통해 음성을 사용하도록 설정했습니다. 다음은 내 appdelegate 코드 처리 알림입니다.신속하게 sinch를 사용하여 알림을받지 못함 3

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) { 
///// 
} 

답변

2

sinch 클라이언트에 장치 토큰을 보내면됩니다.

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