【问题标题】:APNS can't get DeviceTokenAPNS 无法获取 DeviceToken
【发布时间】:2023-03-13 20:00:01
【问题描述】:

我的代码。

UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate                  = self;
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
            if (!error) {
                NSLog(@"~ios10-----");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [[UIApplication sharedApplication] registerForRemoteNotifications];
                });

            }else{
                NSLog(@"~ios10---error--");
            }
        }];

此代码在 iPhone 5s ios 11.4.1 中有效,调用了 didRegisterForRemoteNotificationsWithDeviceToken 方法。 但在 iPhone 6s ios 13.5.0 中不起作用。经过几天的谷歌搜索,APNS connection failure 找到了解决方案,解决方法是在 wifi 设置中更改手机以使用替代 DNS,如 Google (8.8.8.8) 或 Cloudflare (1.1.1.1)。但是我不能修改用户手机的DNS。

如果您有任何想法,请告诉我。谢谢

【问题讨论】:

    标签: ios dns apple-push-notifications devicetoken


    【解决方案1】:

    你可以试试这个,希望对你有帮助。

    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
              UIUserNotificationType allNotificationTypes =
              (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
              UIUserNotificationSettings *settings =
              [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
              [application registerUserNotificationSettings:settings];
          } else {
              // iOS 10 or later
      #if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
              // For iOS 10 display notification (sent via APNS)
              [UNUserNotificationCenter currentNotificationCenter].delegate = self;
              UNAuthorizationOptions authOptions =
              UNAuthorizationOptionAlert
              | UNAuthorizationOptionSound
              | UNAuthorizationOptionBadge;
              [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
              }];
      #endif
          }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 1970-01-01
      相关资源
      最近更新 更多