ios - APNS 无法获取 DeviceToken
问题描述
我的代码。
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 连接失败 中找到了解决方案,解决方法是在 wifi 设置中将手机更改为使用 Google (8.8.8.8) 或 Cloudflare (1.1.1.1) 等替代 DNS。但是我不能修改用户手机的DNS。
如果您有任何想法,请告诉我。谢谢
解决方案
你可以试试这个,希望对你有帮助。
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
}
推荐阅读
- appium - 无法在真实设备上安装集成应用程序
- css - 如何制作一个响应式 css 网格(显示:网格),在顶行有三个相等的框,然后其余行是两个相等大小的框?
- r - 我可以使用基本 tryCatch 在向量上应用函数吗?
- c - 加载二进制文件时,当前数据丢失
- c - 为什么通过pthread_key_create传入的析构函数只被子线程调用,主线程调用?
- matlab - 如何将浮点数转换为整数值以便相互比较?
- python - Python 使用 imaplib/imapclient 与 exchangelib 从 Outlook 帐户读取电子邮件?
- angular - 如何解决 Angular/rxjs/observable 错误?
- java - Java - 尝试制作计时器
- kubernetes - 在 JupyterHub 中指定内存分配?