Skip to content

Commit

Permalink
Merge pull request #20 from sendbird/feature/jaesung/fix-receiving-pu…
Browse files Browse the repository at this point in the history
…shtoken

Fix push notification error after first signing in
  • Loading branch information
Jaesung committed Mar 27, 2020
2 parents 9b547fa + 2bc5de1 commit 45548e2
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions QuickStart.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = QuickStart/QuickStart.entitlements;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = RM4A5PXTUX;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down Expand Up @@ -561,7 +561,7 @@
CODE_SIGN_ENTITLEMENTS = QuickStart/QuickStart.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 12;
CURRENT_PROJECT_VERSION = 17;
DEVELOPMENT_TEAM = RM4A5PXTUX;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand Down
22 changes: 17 additions & 5 deletions QuickStart/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// QR Code Mode
SendBirdCall.configure(appId: appId)
}

SendBirdCall.addDelegate(self, identifier: "DelegateIdentification")

// You must call `SendBirdCall.addDelegate(_:identifier:)` right after configuring new app ID
SendBirdCall.addDelegate(self, identifier: "com.sendbird.calls.quickstart.delegate")

self.voipRegistration()

Expand All @@ -63,13 +64,13 @@ extension AppDelegate: PKPushRegistryDelegate {

// MARK: SendBirdCalls - Registering push token.
func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
UserDefaults.standard.voipPushToken = pushCredentials.token
print("Push token is \(pushCredentials.token.toHexString())")

SendBirdCall.registerVoIPPush(token: pushCredentials.token, unique: true) { error in
guard error == nil else { return }
// Even if an error occurs, SendBirdCalls will save the pushToken value and reinvoke this method internally while authenticating.
}
UserDefaults.standard.pushToken = pushCredentials.token

print("Push token is \(pushCredentials.token.toHexString())")
}

// MARK: SendBirdCalls - Receive incoming push event
Expand All @@ -81,6 +82,17 @@ extension AppDelegate: PKPushRegistryDelegate {
func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) {
// MARK: Handling incoming call
SendBirdCall.pushRegistry(registry, didReceiveIncomingPushWith: payload, for: type) { uuid in
guard uuid != nil else {
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .generic, value: "invalid")
let randomUUID = UUID()
self.provider.reportNewIncomingCall(with: randomUUID, update: update) { error in
self.provider.reportCall(with: randomUUID, endedAt: Date(), reason: .failed)
}
completion()
return
}

completion()
}
}
Expand Down
23 changes: 23 additions & 0 deletions QuickStart/Assets.xcassets/icCallkitSb.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"filename" : "icCallkitSb.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "icCallkitSb@2x.png",
"scale" : "2x",
"idiom" : "universal"
},
{
"scale" : "3x",
"filename" : "icCallkitSb@3x.png",
"idiom" : "universal"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions QuickStart/CXExtensions/CXProvider+QuickStart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
//

import CallKit
import UIKit

extension CXProviderConfiguration {
// The app's provider configuration, representing its CallKit capabilities
static var `default`: CXProviderConfiguration {
let providerConfiguration = CXProviderConfiguration(localizedName: "com.sendbird.calls.quickstart.cxprovider")

let providerConfiguration = CXProviderConfiguration(localizedName: "SendBird Calls")
if let image = UIImage(named: "icCallkitSb") {
providerConfiguration.iconTemplateImageData = image.pngData()
}
providerConfiguration.supportsVideo = false
providerConfiguration.maximumCallsPerCallGroup = 1
providerConfiguration.supportedHandleTypes = [.generic]
Expand Down
7 changes: 3 additions & 4 deletions QuickStart/Extensions/UserDefaults+QuickStart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ extension UserDefaults {
}
}

var pushToken: Data? {
var voipPushToken: Data? {
get {
guard let pushToken = UserDefaults.standard.value(forKey: "com.sendbird.calls.quickstart.pushtoken") as? Data else { return nil }
guard let pushToken = UserDefaults.standard.value(forKey: "com.sendbird.calls.quickstart.pushtoken.voip") as? Data else { return nil }
return pushToken
}
set {
UserDefaults.standard.setValue(newValue, forKey: "com.sendbird.calls.quickstart.pushtoken")
UserDefaults.standard.setValue(newValue, forKey: "com.sendbird.calls.quickstart.pushtoken.voip")
}
}
}
Expand All @@ -76,6 +76,5 @@ extension UserDefaults {
UserDefaults.standard.removeObject(forKey: "com.sendbird.calls.quickstart.user.profile")
UserDefaults.standard.removeObject(forKey: "com.sendbird.calls.quickstart.autologin")
UserDefaults.standard.removeObject(forKey: "com.sendbird.calls.quickstart.accesstoken")
UserDefaults.standard.removeObject(forKey: "com.sendbird.calls.quickstart.pushtoken")
}
}
5 changes: 2 additions & 3 deletions QuickStart/Settings/SettingsTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ class SettingsTableViewController: UITableViewController {
// MARK: - SendBirdCall Interaction
extension SettingsTableViewController {
func signOut() {
guard let token = UserDefaults.standard.pushToken else { return }
guard let token = UserDefaults.standard.voipPushToken else { return }

// MARK: SendBirdCall Deauthenticate
SendBirdCall.deauthenticate(voipPushToken: token) { error in
guard error == nil else { return }
// Removed pushToken successfully
UserDefaults.standard.clear()
guard error == nil else { return }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion QuickStart/SignIn/SignInViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension SignInViewController {

func signIn(userId: String) {
// MARK: SendBirdCall.authenticate()
let params = AuthenticateParams(userId: userId, accessToken: nil, voipPushToken: UserDefaults.standard.pushToken, unique: false)
let params = AuthenticateParams(userId: userId, accessToken: nil, voipPushToken: UserDefaults.standard.voipPushToken, unique: false)
self.startLoading()

SendBirdCall.authenticate(with: params) { user, error in
Expand Down
9 changes: 6 additions & 3 deletions QuickStart/SignIn/SignInWithQRViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ extension SignInWithQRViewController: SignInDelegate {
// Delegate method
func didSignIn(appId: String, userId: String, accessToken: String?) {
SendBirdCall.configure(appId: appId)
// You must call `SendBirdCall.addDelegate(_:identifier:)` right after configuring new app ID
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
SendBirdCall.addDelegate(appDelegate, identifier: "com.sendbird.calls.quickstart.delegate")
}

UserDefaults.standard.appId = appId
UserDefaults.standard.user.id = userId
Expand All @@ -121,7 +125,8 @@ extension SignInWithQRViewController {
func signIn() {
let userId = UserDefaults.standard.user.id
let accessToken = UserDefaults.standard.accessToken
let authParams = AuthenticateParams(userId: userId, accessToken: accessToken, voipPushToken: UserDefaults.standard.pushToken, unique: false)
let voipPushToken = UserDefaults.standard.voipPushToken
let authParams = AuthenticateParams(userId: userId, accessToken: accessToken, voipPushToken: voipPushToken, unique: false)
self.startLoading()

SendBirdCall.authenticate(with: authParams) { user, error in
Expand All @@ -148,7 +153,5 @@ extension SignInWithQRViewController {
self.performSegue(withIdentifier: "signInWithQRCode", sender: nil)
}
}

self.dismiss(animated: true, completion: nil)
}
}

0 comments on commit 45548e2

Please sign in to comment.