简体   繁体   English

如何检测真正的 iOS/APNS 推送令牌何时注册到 Firebase 云消息传递 (FCM)?

[英]How can I detect when the real iOS/APNS push token is registered with Firebase Cloud Messaging (FCM)?

Firebase's didReceiveRegistrationToken as seen below is getting called even if the user hasn't granted push notification permissions.即使用户没有授予推送通知权限,也会调用如下所示的 Firebase 的didReceiveRegistrationToken I need to make sure that APNS push tokens are being registered for analytics, as well as for saving it on my server, but this function is getting called even when the user hasn't granted push permission.我需要确保正在注册 APNS 推送令牌以进行分析,并将其保存在我的服务器上,但是即使用户未授予推送权限,也会调用此 function。 ♂️ ♂️

/**
 * Requirement for Firebase push notifications.
 * See documentation here: https://firebase.google.com/docs/cloud-messaging/ios/client
 */
extension AppDelegate: MessagingDelegate {
    // Note: This callback is fired at each app startup and whenever a new token is generated.
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        let dataDict: [String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        print("Getting called even if the user hasn't opted in for push notifications!")
    }
}

TL;DR : FCM token is not a reliable way to detect if APNS token is registered. TL;DR :FCM 令牌不是检测 APNS 令牌是否已注册的可靠方法。 Disable method swizzling in firebase and listen to APNS registration manually . 禁用 firebase 中的方法 swizzling 并手动监听 APNS 注册

  1. FCM Tokens: An FCM token is also known as a device instance ID; FCM Tokens: FCM 令牌也称为设备实例 ID; it identifies that specific app on that specific device.它标识该特定设备上的特定应用程序。 In other words, FCM token doesn't equal an APNS token.换句话说, FCM 令牌不等于 APNS 令牌。 Here is one explanation: APNs, FCM or GCM token .这是一种解释: APNs、FCM 或 GCM 令牌

  2. FCM Token Auto Generation: Surprisingly, even without iOS user granting permission, Firebase generates an FCM token on app launch. FCM 令牌自动生成:令人惊讶的是,即使没有 iOS 用户授予权限,Firebase 也会在应用启动时生成 FCM 令牌。 I guess it wants to find a way to identify the app & device pair, so as soon as you launch, you're gonna have an FCM token.我猜它想找到一种方法来识别应用程序和设备对,所以一旦你启动,你就会有一个 FCM 令牌。 If you want, you can disable auto-generation and wait for the user to opt-in.如果需要,您可以禁用自动生成并等待用户选择加入。

By default, the FCM SDK generates a registration token for the client app instance on app launch.默认情况下,FCM SDK 在应用启动时为客户端应用实例生成一个注册令牌。 If you want to get an explicit opt-in before using Instance ID, you can prevent generation at configure time by disabling FCM.如果您想在使用实例 ID 之前获得明确的选择加入,您可以通过禁用 FCM 来防止在配置时生成。 To do this, add a metadata value to your Info.plist (not your GoogleService-Info.plist):为此,请将元数据值添加到您的 Info.plist(而不是您的 GoogleService-Info.plist):

Here lies one of the problems!这就是问题之一! Even if the user has disabled auto-generated of FCM tokens, one will be generated when the user is prompted to enable push notifications not when the user has accepted push notification permission.即使用户禁用了 FCM 令牌的自动生成,也会在提示用户启用推送通知时生成一个,而不是在用户接受推送通知权限时生成。 This seems quiet odd and I ended up reporting this to Firebase.这似乎很奇怪,我最终将此报告给 Firebase。

  1. APNS Token Swizzling : So by default Firebase takes over AppDelegate's didRegisterForRemoteNotificationsWithDeviceToken (called "swizzling"). APNS Token Swizzling :所以默认情况下 Firebase 接管 AppDelegate 的didRegisterForRemoteNotificationsWithDeviceToken (称为“swizzling”)。 You can disable this and override the delegate method yourself.您可以禁用它并自己覆盖委托方法。 This will then give you access to both APNS token as well as FCM token.这将使您可以访问 APNS 令牌和 FCM 令牌。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print("Successfully registered for notifications!")
    // Required to make FCM work since we disabled swizzling!
    Messaging.messaging().apnsToken = deviceToken
    // String version of APNS so we can use save it/use it later/do analytics.
    let apnsToken = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
}

Getting a callback for a device ID token and getting a callback for an actual pushed message are two different things.获取设备 ID 令牌的回调和获取实际推送消息的回调是两件不同的事情。 The device ID token callback just gives you a token that identifies the device for a later push notification.设备 ID 令牌回调只是为您提供一个标识设备的令牌,以便稍后推送通知。 It isn't actually a pushed notification itself.它实际上并不是推送通知本身。 It's just an ID.这只是一个身份证。

That said, if you disagree with the behavior of the client SDK from a security perspective, you're free to contact Firebase support directly with feedback.也就是说,如果您从安全角度不同意客户端 SDK 的行为,您可以直接联系 Firebase 支持并提供反馈。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 firebase 云消息的 FCM 令牌有时未注册 - FCM Token for firebase cloud messaging sometimes not registered FCM 检测 IOS 上的应用程序卸载(firebase 云消息推送通知) - FCM detect app uninstall on IOS (firebase cloud messaging push notifications) Firebase Cloud Messaging (FCM) 如何切换 Apple Push Notification service (APNs) 的环境? - How does Firebase Cloud Messaging (FCM) switch the environment of Apple Push Notification service (APNs)? 如何从iOS上的Firebase Cloud Messaging获取令牌 - How can I get the token from Firebase Cloud Messaging on iOS 在iOS Firebase Cloud Messaging(FCM)中,将APNS发送到主题,但发送方不是来自iPhone - In iOS Firebase Cloud Messaging (FCM), send APNS to topic but other than sender from iphone FireBase iOS消息传递,无法获取APNS令牌 - FireBase iOS Messaging, fails to fetch APNS Token IOS应用中的Firebase云消息传递(FCM)无法正常工作 - Firebase cloud messaging (FCM) in IOS app is not working iOS上的Firebase推送通知:FCM说令牌错误:未注册,但应用程序未刷新令牌 - Firebase Push Notifications on iOS: FCM says token error: not registered but app gets no refreshed token Firebase Cloud Messaging将消息发送到IID导入的Apns令牌 - Firebase Cloud Messaging send message to iid imported Apns token 如何在 iOS Swift 中使用 Firebase FCM 令牌在 PubNub 上注册推送通知 - How to register push notification on PubNub with Firebase FCM token in iOS Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM