didUpdatePushCredentials未被调用

Sae*_* N. 5 objective-c ios pushkit

我想在我的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");
}
Run Code Online (Sandbox Code Playgroud)
  • 我启用了远程通知,安装了证书和配置文件.
  • 我可以使用APNS推送标准通知.

任何解决方案让它工作?

Big*_*esy 16

如果您正在运行较新的xcode(我在xcode 9上),那么VOIP不在Capabilities选项卡的Background部分中.这样可以防止didUpdatePushCredentials被叫!

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

我无法相信Apple已经对我们这样做了.我浪费了8个小时的工作日来寻找这个简单的解决方案.

在此输入图像描述

  • 我已经将此添加到info.plist文件中。但是我仍然明白了。 (2认同)

Sae*_* N. 1

证书有问题。

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