ios - 推送通知在首次安装时不起作用
问题描述
我卡在推送通知上。当我第一次安装应用程序时,firebase 通知不会触发,但在杀死一个并再次打开应用程序后它工作正常。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Use Firebase library to configure APIs
FirebaseApp.configure()
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)
application.registerUserNotificationSettings(settings)
}
application.registerForRemoteNotifications()
Messaging.messaging().delegate = self
Messaging.messaging().isAutoInitEnabled = true
return true
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(String(describing: fcmToken))")
UserDefaults.standard.setValue(fcmToken, forKey: "Token")
let dataDict:[String: String] = ["token": fcmToken ]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
解决方案
推荐阅读
- flutter - Flutter - 如何开始列表中的下一个倒计时
- r - 如何从包含特定字符串的数据框中获取列号?
- google-app-engine - 谷歌云端点服务未在 api 库中列出
- excel - 如何为不同的列/工作表循环 VBA 代码
- javascript - Typescript- 具有动态键和预定义键的对象,但具有不同类型的值(或:具有“其余键”定义的对象)
- python - 在 pandas DataFrame 中的字符串中查找子字符串非常慢
- php - 教义,如果ManyToMany关系,如何将附加值传递给连接表行?
- mysql - 如何在 MYSQL 中将表数据转换为 JSon 数组
- javascript - 按钮单击时未清除 Div 容器
- single-sign-on - 如何通过 OIDC 发现 Google 用户是否正在使用两因素身份验证