7

我想在我的 iOS 应用程序中实现 VoIP 通知,但是didUpdatePushCredentials方法从未被调用,我无法获取设备令牌。

我已经在应用程序中实现了 APNS,这两个服务可能会冲突吗?

这是我的 AppDelegate 代码

- (void)application:(UIApplication *)application
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    LOGI(@"%@", NSStringFromSelector(_cmd));

    //register for voip notifications
    self->pushRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
    [self->pushRegistry setDelegate:self];
    [self->pushRegistry setDesiredPushTypes:[NSSet setWithObject:PKPushTypeVoIP]];

    NSLog(@"VoIP push registered");

}

#pragma mark - VoIP push methods

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials:(PKPushCredentials *)credentials forType:(NSString *)type {
    NSLog(@"voip token: %@", credentials.token);
}

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type {
    NSDictionary *payloadDict = [payload.dictionaryPayload valueForKey:@"aps"];
    NSString *message = (NSString *)[payloadDict valueForKey:@"alert"];

    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
        UILocalNotification *localNotification = [[UILocalNotification alloc] init];
        localNotification.alertBody = [message stringByAppendingString:@" - voip"];
        localNotification.applicationIconBadgeNumber = 1;
        localNotification.soundName = @"notes_of_the_optimistic.caf";

        [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VoIP notification" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        });
    }


    NSLog(@"incoming voip notfication: %@", payload.dictionaryPayload);
}

- (void)pushRegistry:(PKPushRegistry *)registry didInvalidatePushTokenForType:(NSString *)type {
    NSLog(@"Voip token invalidate");
}
  • 我启用了远程通知,安装了证书和配置文件。
  • 我可以使用 APNS 推送标准通知。

有什么解决方案让它工作吗?

4

4 回答 4

18

如果您运行的是较新的 xcode(我使用的是 xcode 9),那么 VOIP 不在“功能”选项卡的“背景”部分中。这将防止didUpdatePushCredentials被调用!

诀窍是你必须进入你的 plist,并且Required Background Modes你需要添加App provides Voice over IP services.

我不敢相信苹果已经对我们这样做了。我浪费了 8 个小时的工作日来寻找这个简单的解决方法。

在此处输入图像描述

于 2017-10-26T23:17:15.630 回答
3

对我来说,解决方案是启用:

在此处输入图像描述

PS我没有忘记启用 在此处输入图像描述 但是启用一般推送也很重要。所以完整的代码是:

import UIKit
import PushKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        voipRegistration()

        return true
    }

    // Register for VoIP notifications
    func voipRegistration() {
        let mainQueue = DispatchQueue.main
        // Create a push registry object
        let voipRegistry: PKPushRegistry = PKPushRegistry(queue: mainQueue)
        // Set the registry's delegate to self
        voipRegistry.delegate = self
        // Set the push type to VoIP
        voipRegistry.desiredPushTypes = [.voIP]
    }

}

extension AppDelegate: PKPushRegistryDelegate {
    func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) {
        let token = pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()
        print("voip token = \(token)")
    }

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
        print("payload = \(payload.dictionaryPayload)")
    }
}
于 2019-08-09T08:37:08.823 回答
2

使用下面的代码并确保以下内容。

**`**// Register for VoIP notifications**`**

- (void) voipRegistration {
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // Create a push registry object
    _voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue];
    // Set the registry's delegate to self
    [_voipRegistry setDelegate:(id<PKPushRegistryDelegate> _Nullable)self];
    // Set the push type to VoIP
    _voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
}

在下面调用方法didFinishLaunchingWithOptions

其他删除方法如下

- (void)pushRegistry:(PKPushRegistry *)registry didUpdatePushCredentials: (PKPushCredentials *)credentials forType:(NSString *)type {
    // Register VoIP push token (a property of PKPushCredentials) with server

    if([credentials.token length] == 0) {
        NSLog(@"voip token NULL");
        return;
    }
    NSLog(@"%@",credentials.token);
}

//在项目功能中确保这一点

1)后台模式开启

2)网络电话

3)后台获取

4)远程通知

5) 不要忘记导入委托

在后台模式里面

于 2016-07-04T12:28:03.957 回答
1

证书有问题。

重新生成和重新导入证书并修复问题。

于 2017-02-18T11:59:02.567 回答