如何在VoIP应用程序中使用PushKit后台推送技术

3

目前,我的Voip应用在后台模式下使用以下代码,以保持Voip应用程序在进入后台或进入后台并锁定电话时保持唤醒状态,并在呼叫到达时显示通知。我发现在iOS 8及更高版本中,setKeepAliveTimeout不再起作用,我们需要使用Pushkit,有没有办法在不编写服务器端代码的情况下使用PushKit?

- (void)registerKeepAliveHandler:(UIApplication*) application
{
    LogInfo(TAG_MAIN, @"Registering KeepAliveTimeout");
    [application setKeepAliveTimeout:600 handler:^{
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_KEEPALIVE_RECEIVED object:nil];
        NSMutableString* statusStr = [[NSMutableString alloc] init];
        switch ([[UIApplication sharedApplication] applicationState]) {
            case UIApplicationStateActive:
                [statusStr appendString:@"foreground"];
                break;
            case UIApplicationStateInactive:
                [statusStr appendString:@"inactive"];
                break;
            case UIApplicationStateBackground:
                [statusStr appendString:@"background"];
                break;
            default:
                assert(0);
                break;
        }
        LogInfo(TAG_MAIN, @"===================================== Keepalive received in %@ application status ===============================", statusStr);
        [UIApplication getIPAddress];
        LogInfo(TAG_MAIN, @"%@", [[UIDevice currentDevice] batteryInfo]);
        [[SipEngine sharedInstance] isServerReachable];
        [[SipEngine sharedInstance] isRegistered];
        [[SipEngine sharedInstance] startStopRegistration];
        [[[SipEngine sharedInstance] registration] registering];
    }];

}
2个回答

1
如果您想让应用程序在被杀死的状态下工作,并了解来电并进行进一步处理,则应使用Push kit静默通知。 (这是WhatsApp呼叫,Skype等应用程序的工作方式)。 参考
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    pushRegistry.delegate = self;
    pushRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];

    return YES;
}

#define PushKit Delegate Methods

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type{
    if([credentials.token length] == 0) {
        NSLog(@"voip token NULL");
        return;
    }

    NSLog(@"PushCredentials: %@", credentials.token);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type
{
    NSLog(@"didReceiveIncomingPushWithPayload");
}


@end

基于推送负载的本地通知调度方法


嗨@Hasya,谢谢。如果我想使用上面的函数,而不是NSLog(@"didReceiveIncomingPushWithPayload");,你有任何想法我需要使用什么吗?因为这样它不起作用。 - Steven
欢迎@Steven,如果我的回答对您有帮助,请点赞并接受它。随时让我知道我能否帮到您。 - Hasya
你能接收Pushkit负载吗?如果可以,那么根据Pushkit负载,您可以安排带有来电声音的本地通知,还可以添加交互事件以接受或拒绝呼叫。https://github.com/hasyapanchasara/PushKit_SilentPushNotification/issues/1 - Hasya

0

在iOS中,前进时,使用Pushkit作为后台(BG)Voip应用程序是强制性的,因为它将完全废弃iOS 11中的Voip BG套接字。(我认为他们已经在iOS 10 SDK中停止了支持,但iOS 10可以使用SDK 9)

回答您的问题,不需要,在您的服务器端实现APNS接口与苹果的APNS云通信,以便您的应用程序可以通过Apple接收Voip呼叫等Voip推送。此外,请注意,如果处于封闭网络(无互联网),则iOS中的VOIP应用程序将不再工作,因为APNS接口是必需的,以将Voip推送传递到使用Pushkit的应用程序。


嗨@Ayush,我找不到任何关于如何在服务器端实现APNS接口以与苹果的APNS云通信的教程。有没有什么方法可以找到一些如何做到这一点的教程? - Steven
有点奇怪。苹果实际上已经提供了完整的文档。https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1 - Ayush

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