ios - didUpdatePushCredentials 没有被调用

标签 ios objective-c pushkit

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

我在应用程序中实现了 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 推送标准通知。

有什么解决方案可以让它正常工作吗?

最佳答案

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

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

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

enter image description here

关于ios - didUpdatePushCredentials 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37099280/

相关文章:

ios - 您如何处理具有非 Apple 帐户系统的应用程序中的自动续订订阅?

ios - 日语日期格式 ios

ios - 苹果HLS离线视频播放如果网络不可用则无法播放2个视频

ios - IBOutletCollection 对象索引错误

iphone - 如何在 viewWillAppear 中重新加载表数据?

强制关闭后收到 pushkit voip 通知时 IOS 应用程序崩溃

IOS Swift Pushkit : didUpdatePushCredentials not called

iOS PushKit 注册在 iOS10 上不起作用

objective-c - "Official"在 Objective-C 中引用类方法的语法?

objective-c - 如何在 iOS 中制作 "Identify browser as"