2

I integrated Sinch sdk into my projects with VoIP, PushKit, CallKit. Everything is working fine. But when app is not running in device A and someone(Device B) is calling to it then sometimes in device A I am getting notification with text SIN_INCOMING_CALL . I don't know from where it is coming. But my problem is, I want to open default call screen of iOS device. Sometime it is being opened and sometimes it show notification with this text.

My Appdelegate has following code.

@interface AppDelegate() <SINClientDelegate, SINCallClientDelegate, SINManagedPushDelegate, SINCallDelegate>

@property (nonatomic, readwrite, strong) id<SINManagedPush> push;
@property (strong, nonatomic) id<SINClient> sinchClient;
@property (nonatomic, readwrite, strong) SINCallKitProvider *callKitProvider;

@end

@implementation AppDelegate

- (void)applicationWillEnterForeground:(UIApplication *)application
    {
        id<SINCall> call = [self.callKitProvider currentEstablishedCall];
        if (call) {
              //Show call view controller and pass call
        }
    }


#pragma mark - Remote Notification

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
    if (notificationSettings.types != UIUserNotificationTypeNone) { [application registerForRemoteNotifications]; }
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSString *newToken = deviceToken.description;
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
    [self.push application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    NSLog(@"My token is: %@", newToken);

    //get previously initiated Sinch client
    if (!self.sinchClient) { [self initSinchCallConfig]; }
    id<SINClient> client = [self sinchClient];
    [client registerPushNotificationData:deviceToken];
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    NSLog(@"Failed to get token, error: %@", error);
    [obj_SharedModel setUDID:@""]; //Statuc Device Id just for testing purpose from simulator.
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [self.push application:application didReceiveRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)registerForPushNotifications
{
    UIApplication *application = [UIApplication sharedApplication];
    if (IS_OS_10_BELOW) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound) categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    else {
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
            if(!error){ dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] registerForRemoteNotifications]; }); }
        }];
    }

    //register for VoIP push
    PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    voipRegistry.delegate = self;
    voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

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

- (void)unRegisterForPushNotifications
{
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
    [self.sinchClient unregisterPushNotificationDeviceToken];
}

#pragma mark - IOS 10 UP - UNUserNotificationCenter

//Called when a notification is delivered to a foreground app.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    NSLog(@"User Info : %@",notification.request.content.userInfo);
    completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

//Called to let your app know which action was selected by the user for a given notification.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    completionHandler();
}

#pragma mark - VoIP remote Notifications

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
    NSString *newToken = credentials.token.description;
    newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSLog(@"VoIP Tokenp is: %@", newToken);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
    NSDictionary *dictPayload = payload.dictionaryPayload;
    if ([dictPayload sin_isSinchPushPayload]) {
        if (!self.sinchClient) { [self initSinchCallConfig]; }
        [self.sinchClient relayRemotePushNotification:dictPayload];

        /*
         {"aps":{"alert":{"loc-key" : "SIN_INCOMING_CALL"}},"sin" :"ASDAS" }
         */
    }
    else {
    }
}

- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type { }

#pragma mark - SINClient Method

- (void)initSinchCallConfig
{
    // Instantiate a Sinch client object
    if (!self.sinchClient && [stLoginUserName length]) {
        self.sinchClient = [Sinch clientWithApplicationKey:@"dea827c1-1208-45a6-b510-5d1f277d9607" applicationSecret:@"AgxDpf/4X0OrLx1GXv6Quw==" environmentHost:@"sandbox.sinch.com" userId:stLoginUserName];
        self.sinchClient.delegate = self;
        self.sinchClient.callClient.delegate = self; //for incomming call

        [self.sinchClient setSupportCalling:YES];
        [self.sinchClient setSupportPushNotifications:YES];
        [self.sinchClient setPushNotificationDisplayName:MSG_TITLE];
        [self.sinchClient enableManagedPushNotifications];

        [self.sinchClient start];
        [self.sinchClient startListeningOnActiveConnection];

        self.callKitProvider = [[SINCallKitProvider alloc] initWithClient:self.sinchClient];
    }
}

#pragma mark - SINClient Delegate

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

- (void)clientDidFail:(id<SINClient>)client error:(NSError *)error {
    NSLog(@"Sinch client error: %@", [error localizedDescription]);
}

- (void)client:(id<SINClient>)client logMessage:(NSString *)message area:(NSString *)area severity:(SINLogSeverity)severity timestamp:(NSDate *)timestamp {
    NSLog(@"%@", message);
}

#pragma mark - SINCallClient Delegate

- (void)client:(id<SINCallClient>)client didReceiveIncomingCall:(id<SINCall>)call {
    NSLog(@"didReceiveIncomingCall");
    CallViewController *callVC = [[CallViewController alloc] initWithNibName:@"CallViewController" bundle:nil];
    callVC.call = call;
    AppNavigationController *nav = [[AppNavigationController alloc] initWithRootViewController:callVC];
    [self.navigationController presentViewController:nav animated:YES completion:nil];

    if (call.details.applicationStateWhenReceived == UIApplicationStateActive) { }
    else { [callVC btnAcceptCallAction]; }
}

- (void)client:(id<SINClient>)client willReceiveIncomingCall:(id<SINCall>)call {
    [self.callKitProvider reportNewIncomingCall:call];
    call.delegate = self;
}

#pragma mark - SINManagedPush Delegate

- (void)managedPush:(id<SINManagedPush>)unused didReceiveIncomingPushWithPayload:(NSDictionary *)payload forType:(NSString *)pushType {
    if (!self.sinchClient) { [self initSinchCallConfig]; }
    [self.sinchClient relayRemotePushNotification:payload];
}

- (void)call:(id<SINCall>)call shouldSendPushNotifications:(NSArray *) pushPairs {
    NSLog(@"shouldSendPushNotifications");
}

#pragma mark - SINCall Delegate

- (void)callDidProgress:(id<SINCall>)call {

}

- (void)callDidEstablish:(id<SINCall>)call {

}

- (void)callDidEnd:(id<SINCall>)call { [[self callKitProvider] reportCallEnded:call]; }
@end
Kishan Vyas
  • 327
  • 1
  • 2
  • 15

0 Answers0