Search code examples
iosobjective-cios11sinchpushkit

VOIP Sinch in iOS 11 not work in background


I use Sinch VOIP to make calling between App-To-App in iOS 11.1.2
Work well when the application status is foreground but in background and terminated nothing happened after I open the application console print

Pubnub request SCHEDULED (ID=081E49C2-C30A-4B4B-840C-E6A6051E6F44, URL=https://rebtelsdk.pubnub.com/subscribe/sub-c-c5e52f20-d446-11e3-b488-02ee2ddab7fe/5e1e1309-136a-40d4-935f-2627ebe4e8f2B/0/0, NST-VoIP: NO)


Pubnub request STARTED (ID=081E49C2-C30A-4B4B-840C-E6A6051E6F44)
Pubnub request SUCCESS (ID=081E49C2-C30A-4B4B-840C-E6A6051E6F44):
(
        (
    ),
    15117251992031337
)

onPubSubSubscriptionSuccess: userInfo: {
    channel = "5e1e1309-136a-40d4-935f-2627ebe4e8f2B";
    subscribeSequence = 1;
    timetoken = 0;
    useVoIPNetworkServiceType = 0;
}

I upload VOIP & APNS certificates to Sinch dashboard and I used SINManagedPush & PushKit My code is

  1. Setup Push Manager & SINClient in didFinishLaunchingWithOptions
    self.push = [Sinch managedPushWithAPSEnvironment:SINAPSEnvironmentAutomatic];
    self.push.delegate = self;
    [self.push setDesiredPushTypeAutomatically];
    void (^onUserDidLogin)(NSString *) = ^(NSString *userId) {
    [self.push registerUserNotificationSettings];
    [self initSinchClientWithUserId:userId];
    };
    [[NSNotificationCenter defaultCenter]
    addObserverForName:@"UserDidLoginNotification"
    object:nil
    queue:nil
    usingBlock:^(NSNotification *note) {
     NSString *userId = note.userInfo[@"userId"];

     [[NSUserDefaults standardUserDefaults] setObject:userId     forKey:@"userId"];
     [[NSUserDefaults standardUserDefaults] synchronize];
     onUserDidLogin(userId);
     }];


     - (void)initSinchClientWithUserId:(NSString *)userId {
    if (!_client) {
    _client = [Sinch clientWithApplicationKey:@"APP-Key"
                            applicationSecret:@"APP-Secret"
                              environmentHost:@"sandbox.sinch.com"
                                       userId:userId];

    _client.delegate = self;
    _client.callClient.delegate = self;
    [_client setSupportCalling:YES];
    [_client enableManagedPushNotifications];

    [_client start];
    [_client startListeningOnActiveConnection];
    _callKitProvider = [[SINCallKitProvider alloc] initWithClient:_client];


    }
    }
  1. Get Device Token
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{


[self.push application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"User Info : %@",notification.request.content.userInfo);
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);

[self.push application:[UIApplication sharedApplication] didReceiveRemoteNotification:notification.request.content.userInfo];
}


-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"User Info : %@",response.notification.request.content.userInfo);
completionHandler();
[self.push application:[UIApplication sharedApplication] didReceiveRemoteNotification:response.notification.request.content.userInfo];
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[self.push application:application didReceiveRemoteNotification:userInfo];
}
  1. PushKit
-(void)voipRegistration
{
PKPushRegistry* voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}

 -(void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type
{

[_client registerPushNotificationData:credentials.token];
 }
 -(void)pushRegistry:(PKPushRegistry *)registry   didInvalidatePushTokenForType:(PKPushType)type{


NSLog(@"invalidated");

 }

4.Call voipRegistration when SINClient start

- (void)clientDidStart:(id<SINClient>)client {
NSLog(@"Sinch client started successfully (version: %@)", [Sinch version]);
[self voipRegistration];
}

5.Implement SINManagedPushDelegate & SINCallClientDelegate

- (void)client:(id<SINCallClient>)client didReceiveIncomingCall:(id<SINCall>)call {

UIViewController *top = self.window.rootViewController;
while (top.presentedViewController) {
    top = top.presentedViewController;
}
[top performSegueWithIdentifier:@"callView" sender:call];
}

- (SINLocalNotification *)client:(id<SINClient>)client localNotificationForIncomingCall:(id<SINCall>)call {
SINLocalNotification *notification = [[SINLocalNotification alloc] init];
NSArray * ansAr = @[@"رد",@"Answer"];
NSArray * MsgAr = @[[NSString stringWithFormat:@"مكالمة لم يرد عليها من %@", [call remoteUserId]],[NSString stringWithFormat:@"Incoming call from %@", [call remoteUserId]]];
notification.alertAction = ansAr[self.languageID];
notification.alertBody = MsgAr[self.languageID];
return notification;
}
- (void)client:(id<SINClient>)client willReceiveIncomingCall:(id<SINCall>)call {

[self.callKitProvider reportNewIncomingCall:call];
}

These is the code , Please help me if I forgot anything.
I checked the credentials.token is not null.
Thank you for your help.


Solution

  • When you setup the Sinch managed push with the code below:

    self.push = [Sinch managedPushWithAPSEnvironment:SINAPSEnvironmentAutomatic];
    self.push.delegate = self;
    [self.push setDesiredPushTypeAutomatically];
    

    The Sinch SDK will automatically do all the PushKit registration for you. So step 3 and 4 in your description is not necessary and should not be there. Part of the code in step 2 is not needed as well. Please take a look at the Sinch CallKit sample app from the SDK download package, and refer to the implementation of that App.

    Below is a demo video made with the 3.12.4 Sinch SDK CallKit Sample App without any tweak, the device used in the demo is an iPhone7 running iOS 11, it gets the incoming call in background, killed and lockscreen mode:

    Sinch CallKit Sample App Demo Video

    Also, note that callKit only works with VoIP push, have you uploaded the right type of push certificate to Sinch Portal?enter image description here