【问题标题】:didUpdatePushCredentials not get called没有调用 didUpdatePushCredentials
【发布时间】:2016-05-08 11:26:43
【问题描述】:

我想在我的 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 推送标准通知。

有什么办法让它工作吗?

【问题讨论】:

    标签: ios objective-c pushkit


    【解决方案1】:

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

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

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

    【讨论】:

    • 我已经将此添加到 info.plist 文件中。但我仍然得到这个。
    【解决方案2】:

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

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

    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)")
        }
    }
    

    【讨论】:

      【解决方案3】:

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

      **`**// 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中调用Below方法

      其他删除方法如下

      - (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) 不要忘记导入委托

      在后台模式中

      【讨论】:

      • 我按照相同的步骤进行操作,Sinch 也提到了这些步骤。但仍然面临同样的问题。
      【解决方案4】:

      证书有问题。

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

      【讨论】:

      • @KishanVyas 创建推送证书时需要证书,您确定选择了正确的证书吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 2013-11-06
      • 2014-11-28
      相关资源
      最近更新 更多