使用Pushkit和Sinch VOIP无法工作的解决方法

6
我正在尝试在我的IOS应用中使用Sinch实现App-to-App通话。我已经在我的iOS应用程序中使用Sinch实现了Pushkit,但当应用程序处于后台时推送通知无法正常工作。
我有两个问题:
1. 我需要另一个Web服务来单独发送推送通知以接收传入的应用程序吗,还是Sinch自己处理它?
2. 如果它会自己处理,那么我在代码中缺少什么?
#import "AppDelegate.h"
@interface AppDelegate ()
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

    [self handleLocalNotification:[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]];

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

    [self.push registerUserNotificationSettings];

    return YES;
 }
 - (BOOL)application:(UIApplication *)app
        openURL:(NSURL *)url
        options:(NSDictionary *)options {
    return [[GIDSignIn sharedInstance] handleURL:url
                           sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
                                  annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
  }

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
    return [[GIDSignIn sharedInstance] handleURL:url
                           sourceApplication:sourceApplication
                                  annotation:annotation];
}

- (id<SINClient>)client {
   return _sinchClient;
}
-(void)clientDidFail:(id<SINClient>)client error:(NSError *)error{

   NSLog(@"fail");
}

-(void)clientDidStart:(id<SINClient>)client{

    NSLog(@"Start");
    [self voipRegistration];
}


- (void)client:(id<SINClient>)client
   logMessage:(NSString *)message
      area:(NSString *)area
    severity:(SINLogSeverity)severity
   timestamp:(NSDate *)timestamp {
// If you want all messages remove the if statement

    if (severity == SINLogSeverityCritical) {
        NSLog(@"%@", message);
    }
}

- (void)initSinchClientWithUserId:(NSString *)userId {
    if (!_sinchClient) {

        _sinchClient = [Sinch clientWithApplicationKey:@"<my-key>"
                                applicationSecret:@"<my-secret>"
                                  environmentHost:@"sandbox.sinch.com"
                                           userId:userId];

        _sinchClient.delegate = self;

        [_sinchClient setSupportCalling:YES];
        [_sinchClient startListeningOnActiveConnection];
        [_sinchClient enableManagedPushNotifications];
        [_sinchClient start];
    }
 }
 - (void)handleLocalNotification:(UILocalNotification *)notification {
    if (notification) {
        id<SINNotificationResult> result = [self.sinchClient      relayLocalNotification:notification];
        if ([result isCall] && [[result callResult] isTimedOut]) {
            UIAlertView *alert = [[UIAlertView alloc]
                                  initWithTitle:@"Missed call"
                                  message:[NSString stringWithFormat:@"Missed call from %@", [[result callResult] remoteUserId]]
                                  delegate:nil
                                  cancelButtonTitle:nil
                                  otherButtonTitles:@"OK", nil];
            [alert show];
        }
    }
}

-(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
{
    [_sinchClient registerPushNotificationData:credentials.token];
}
-(void)pushRegistry:(PKPushRegistry *)registry   didInvalidatePushTokenForType:(PKPushType)type{

     NSLog(@"invalidated");
}
-(void)pushRegistry:(PKPushRegistry *)registry   didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:  (NSString *)type
{
    //notify
    NSDictionary* dic = payload.dictionaryPayload;
    NSString* sinchinfo = [dic objectForKey:@"sin"];
    UILocalNotification* notif = [[UILocalNotification alloc] init];
    notif.alertBody = @"incoming call";
    [[UIApplication sharedApplication] presentLocalNotificationNow:notif];
    if (sinchinfo == nil)
        return;

    dispatch_async(dispatch_get_main_queue(), ^{
        [_sinchClient relayRemotePushNotificationPayload:sinchinfo];
    });
}

您是否已将Voip证书上传至Sinch仪表板? - cjensen
@WaleedVic,我也遇到了同样的问题。当应用程序在后台运行时,Sinch推送通知无法正常工作。你找到解决方案了吗?谢谢。 - gstream
@gstream79 ,不,先生,我没有这样做,我给那些人发了电子邮件,他们说他们正在处理,很快就会出来,但不知道有多快。 - Achilles
@gstream79,你真是个天才。能请教一下解决方案吗? - Achilles
@WaleedVicm,刚刚将答案添加为解决方案。 - gstream
显示剩余6条评论
1个回答

4
如果您集成了Pushkit和Sinch,则推送通知可能无法在PushKit的didReceiveIncomingPushWithPayload委托函数中捕获。但是,您可以在SINManagedPushDelegate的didReceiveIncomingPushWithPayload函数中获得推送通知。 当应用程序处于后台时,推送通知不会到来,但您可以在那里获取呼入电话事件。如果应用程序在后台,则可以触发本地通知,让用户知道有来电。 希望这对您有所帮助。

因为didReceiveIncomingPushWithPayload委托被一个问题最多的人在他的问题中提到,但他仍然没有接收到推送。 - Shakeel Ahmed
尽管应用退出时推送仍未接收,但我在这个问题上有一个小任务需要您的帮助,请评论并在此留下Skype ID。 - Shakeel Ahmed
SinManagedPush + CallKit 在 iOS 13 中完美运行。 - gstream
@MeetIosDeveloper,你有什么问题? - gstream
@gstream79 因为在接收到 PushKit VoIP 推送回调后从未将来电发布到系统,所以终止应用程序。 - Meet Ios Developer
显示剩余8条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接