gpt4 book ai didi

ios - Firebase FCM 最初不工作,但如果打开应用程序,则在 3-4 次后工作

转载 作者:可可西里 更新时间:2023-11-01 00:56:26 25 4
gpt4 key购买 nike

我是 Firebase FCM 集成的新手,我遇到了一些问题。第一次安装该应用程序时,我没有收到推送通知,但如果我打开和关闭应用程序几次,然后从 Firebase 发送推送通知,我将收到通知,而客户端或服务器代码没有任何更改。谁能帮我解决这个问题?

我在下面附上了我的 appdelegate 代码

import UIKit
import Firebase
import UserNotifications
import FirebaseMessaging
import FirebaseInstanceID

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,UNUserNotificationCenterDelegate,MessagingDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {


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: { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
})
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}

application.registerForRemoteNotifications()
return true
}

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
UIApplication.shared.registerForRemoteNotifications()
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// I have tried both method but none worked and i also tried MessagingAPNSTokenType.sandbox and prod
// Method 1: Messaging.messaging().apnsToken = deviceToken
// Method 2:
Messaging.messaging().setAPNSToken(deviceToken, type: MessagingAPNSTokenType.unknown)
}
}

提前致谢

最佳答案

我遇到了与您相同的问题,并且有一个已知问题 FCM token并不总是与 APNs device token 相关联如果UIApplication.shared.registerForRemoteNotifications()调用不够早。

根据 this GitHub 线程此问题已在 FirebaseInstanceID SDK 中修复,应该很快就会出来。

与此同时,您可以:

锁定您的 FirebaseInstanceID pod to 2.0.0在你的 Podfile 中,或者

确保您正在调用 UIApplication.shared.registerForRemoteNotifications()在应用程序启动时,最好在 FirebaseApp.configure() 之前.

更新:
我刚刚做了一些额外的测试,我注意到这在什么时候起作用有点随机。对于某些设备,它可以立即用于某些设备,而对于某些则不能。对于某些设备 Single Token push工作而不是User Segment .我设法让它工作,这样你至少要么得到 Single Token pushUser Segment通过添加以下内容:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in
})

// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
Messaging.messaging().delegate = self
application.registerForRemoteNotifications()
print("::: registerForRemoteNotifications iOS 10")

} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
print("::: registerUserNotificationSettings iOS 9")
}

FirebaseApp.configure()
Messaging.messaging().delegate = self
Messaging.messaging().shouldEstablishDirectChannel = true

if let refreshedToken = InstanceID.instanceID().token() {
print("::: InstanceID token: \(refreshedToken)")
}

NotificationCenter.default.addObserver(self, selector: #selector(tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil)
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().setAPNSToken(deviceToken, type: .prod)

if let refreshedToken = InstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
}

问题是,如果您不访问 didRegisterForRemoteNotificationsWithDeviceToken,它将无法工作。功能。

关于ios - Firebase FCM 最初不工作,但如果打开应用程序,则在 3-4 次后工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46578563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com