ios - Swift iOS 13,使用移动网络时未获取 APNS 设备令牌(4g/3g)
问题描述
我试图获取 APNS 推送令牌。
func configPushNotifications(_ application: UIApplication) {
application.registerForRemoteNotifications()
}
但是如果我使用的是 My Phone Sim Internet (4g/3g),我没有从 AppDelegate 收到任何令牌。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
但如果我使用 Wifi,它工作正常。我检查了iOS 13.1.2
和13.1.3
。两者都有同样的问题。但较低的版本喜欢iOS 12 or 11
工作正常。是苹果虫吗?或者我必须为移动网络请求具有不同配置的令牌?
解决方案
请验证代码,如下所示
首先导入本地通知
import UserNotifications
然后创建一个方法
func settingPushNotification() {
let app = UIApplication.shared
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
app.registerUserNotificationSettings(settings)
}
app.registerForRemoteNotifications()
}
您可以以这种方式appdelegate
或以viewcontroller
这种方式调用此方法。
self.settingPushNotification()
您需要添加委托方法
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
if !token.isEmpty {
let userDefaults = UserDefaults.standard
userDefaults.set(token, forKey: Strings.DeviceToken.rawValue)
}
print("Device Token: \(token)")
}
func application(
_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
确保您在签名和功能中添加了推送通知。
这样您就可以获得 APNS 设备令牌。
推荐阅读
- php - 将旧的 mysql 调用重写为新的 mysqli_real_query
- amazon-web-services - 发送 POST 请求返回“文本为空”
- javascript - 如何获得全时使用 Javascript 显示
- javascript - 如何在不忽略重定向的情况下将道具传递给私有路由
- node.js - PreSignUP 在 AWS Lambda 函数中因错误连接 ECONNREFUSED 127.0.0.1:443 而失败
- pentaho - 在 Pentaho 数据集成中按字段名称而不是位置读取 CSV 输入文件
- python - 电影票费用计算器
- r - 使用地图函数更改数据帧的编码
- python - 为美丽的汤写这个的正确方法是什么
- html - 侧栏中的响应式页脚重叠 li 标签