feat(ios): emit tokenReceived event when FCM token is issued#190
Open
eljass wants to merge 1 commit into
Open
Conversation
Adds a `tokenReceived` event JS clients can subscribe to for live FCM registration token updates on iOS. Addresses the long-standing request in capacitor-community#177. Implementation: - Plugin.swift `messaging(_:didReceiveRegistrationToken:)` now calls `notifyListeners("tokenReceived", ["token": token])` - Gated on `Messaging.messaging().apnsToken != nil` so the pre-APNs registration token Firebase mints on first launch (which gets immediately replaced) is not emitted to consumers - Deduped against `lastNotifiedToken` so foreground/background transitions do not re-emit the same token JS API: FCM.addListener('tokenReceived', ({ token }) => { ... }); `getToken()` behaviour is unchanged; this PR is purely additive. No new dependencies. The Android side will arrive in a follow-up PR.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a
tokenReceivedevent JS clients can subscribe to for live FCM registration token updates on iOS. Partially addresses #177 (Android follow-up planned).Implementation:
Plugin.swiftmessaging(_:didReceiveRegistrationToken:)now callsnotifyListeners("tokenReceived", ["token": token])Messaging.messaging().apnsToken != nilso the pre-APNs registration token Firebase mints on first launch (which gets immediately replaced) is not emitted to consumers — this prevents persisting a stale token in the consumer's backendlastNotifiedTokenso foreground/background transitions do not re-emit the same tokenJS API:
Why
Issue #177 asks for a way to monitor token refresh without polling
getToken(). iOS already had aMessagingDelegatecallback wired up — it just discarded the token after caching. This change forwards the value to JS.Test plan
PushNotifications.register()resolves (post-APNs)Notes
getToken()behaviour is unchanged. Purely additive.getToken()once afterPushNotifications.register()resolves, in case Firebase has already minted (and cached) a token before the listener attaches. Tokens minted before the listener registers are not buffered in this PR — adding a buffer would be a larger behavioural change and is left for a separate discussion.@capacitor/push-notificationsas a peer dependency, since FCM and PushNotifications cannot both register aFirebaseMessagingServiceindependently). Issue How to Monitor token refresh on android/ios using this plugin #177 will close once both land.Related PR
This stacks on top of #189 (
fix(ios): refreshToken race), but the diffs do not overlap — both can be reviewed and merged independently.