didUpdatePushCredentials 没有被调用

didUpdatePushCredentials not get called

我想在我的 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");
}

有什么办法让它工作吗?

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

**`**// 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) 别忘了导入委托

后台模式里面

证书有问题。

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

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

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

我无法相信 Apple 会这样对我们。我浪费了一个 8 小时的工作日来寻找这个简单的修复方法。

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

P.S。我没有忘记启用 但启用一般推送也很重要。所以完整的代码是:

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", [=10=]) }.joined()
        print("voip token = \(token)")
    }

    func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType) {
        print("payload = \(payload.dictionaryPayload)")
    }
}