带有Sinch VOIP的Pushkit不能与pushkit一起使用 [英] Pushkit with Sinch VOIP not working with pushkit

查看:104
本文介绍了带有Sinch VOIP的Pushkit不能与pushkit一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的IOS应用中使用Sinch实现应用到应用的调用.我已经在Sinch的iOS应用中实现了Pushkit,但是当应用处于后台时,推送通知不起作用.

I am trying to implement App-to-App calling with Sinch in my IOS app. I have implemented Pushkit in my iOS app with Sinch but the push notification is not working when the app is in background.

我有两个问题.

  1. 我是否需要另一个Web服务将推送通知分别发送到我的应用程序以接收传入的应用程序,还是Sinch自行处理.

  1. Do I need another web service to send push notification to my app for incoming app separately or Sinch handles it itself.

如果它能自行处理,那么我的代码中缺少什么.

If it does handle itself then what am I missing in my code.

#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];
    });
}

推荐答案

如果您将Pushkit和Sinch集成在一起,则推送通知可能无法抓住PushKit的委托函数-didReceiveIncomingPushWithPayload.但是您可以在SINManagedPushDelegate的功能-didReceiveIncomingPushWithPayload上获得推送通知. 推送通知不会到来,但是当应用程序处于后台时,您可以在那里收到来电事件.如果应用程序在后台运行,则可以触发本地通知,以通知用户来电.

If you integrated Pushkit and Sinch then push notification may can't catch on PushKit's delegate function - didReceiveIncomingPushWithPayload. But you can get push notification on SINManagedPushDelegate's function - didReceiveIncomingPushWithPayload. Push notification is not coming but you can get incoming call event on there when app is in background. You can trigger local notification if app is in background to let user incoming call know.

希望对您有帮助.

这篇关于带有Sinch VOIP的Pushkit不能与pushkit一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆