Skip to content

Commit 1227d0d

Browse files
authored
feat(app-check): support tokenChanged listener on Android and iOS (#855)
* feat(app-check): support tokenChanged Add support on Android. * feat(app-check): support tokenChanged Add support on iOS. * feat(app-check): support tokenChanged Generate changeset.
1 parent 22c4c13 commit 1227d0d

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
lines changed

.changeset/tiny-walls-join.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@capacitor-firebase/app-check': minor
3+
---
4+
5+
feat: implement support for `tokenChanged` listener on Android and iOS

packages/app-check/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ addListener(eventName: 'tokenChanged', listenerFunc: TokenChangedListener) => Pr
160160

161161
Called when the App Check token changed.
162162

163-
Only available for Web.
164-
165163
| Param | Type |
166164
| ------------------ | --------------------------------------------------------------------- |
167165
| **`eventName`** | <code>'tokenChanged'</code> |

packages/app-check/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/appcheck/FirebaseAppCheck.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package io.capawesome.capacitorjs.plugins.firebase.appcheck;
22

33
import android.util.Log;
4+
import androidx.annotation.NonNull;
5+
import com.google.firebase.appcheck.FirebaseAppCheck.AppCheckListener;
46
import com.google.firebase.appcheck.debug.DebugAppCheckProviderFactory;
57
import com.google.firebase.appcheck.playintegrity.PlayIntegrityAppCheckProviderFactory;
68

79
public class FirebaseAppCheck {
810

11+
@NonNull
12+
private final FirebaseAppCheckPlugin plugin;
13+
14+
@NonNull
15+
private final AppCheckListener appCheckListener;
16+
17+
public FirebaseAppCheck(@NonNull FirebaseAppCheckPlugin plugin) {
18+
this.plugin = plugin;
19+
this.appCheckListener = this.plugin::handleTokenChanged;
20+
getFirebaseAppCheckInstance().addAppCheckListener(appCheckListener);
21+
}
22+
923
public void getToken(boolean forceRefresh, final GetTokenResultCallback resultCallback) {
1024
getFirebaseAppCheckInstance()
1125
.getAppCheckToken(forceRefresh)

packages/app-check/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/appcheck/FirebaseAppCheckPlugin.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@
66
import com.getcapacitor.PluginCall;
77
import com.getcapacitor.PluginMethod;
88
import com.getcapacitor.annotation.CapacitorPlugin;
9+
import com.google.firebase.appcheck.AppCheckToken;
910

1011
@CapacitorPlugin(name = "FirebaseAppCheck")
1112
public class FirebaseAppCheckPlugin extends Plugin {
1213

1314
public static final String TAG = "FirebaseAppCheck";
1415
public static final String ERROR_ENABLED_MISSING = "enabled must be provided.";
16+
public static final String TOKEN_CHANGED_EVENT = "tokenChanged";
1517

16-
private FirebaseAppCheck implementation = new FirebaseAppCheck();
18+
private final FirebaseAppCheck implementation = new FirebaseAppCheck(this);
1719

1820
@PluginMethod
1921
public void getToken(PluginCall call) {
@@ -84,4 +86,10 @@ public void setTokenAutoRefreshEnabled(PluginCall call) {
8486
call.reject(exception.getMessage());
8587
}
8688
}
89+
90+
public void handleTokenChanged(AppCheckToken token) {
91+
JSObject result = new JSObject();
92+
result.put("token", token.getToken());
93+
notifyListeners(TOKEN_CHANGED_EVENT, result, true);
94+
}
8795
}

packages/app-check/ios/Plugin/FirebaseAppCheck.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ import FirebaseCore
44
import FirebaseAppCheck
55

66
@objc public class FirebaseAppCheck: NSObject {
7-
override init() {
7+
private let plugin: FirebaseAppCheckPlugin
8+
9+
public init(plugin: FirebaseAppCheckPlugin) {
10+
self.plugin = plugin
11+
12+
super.init()
13+
814
if FirebaseApp.app() == nil {
915
FirebaseApp.configure()
1016
}
17+
18+
NotificationCenter.default.addObserver(self, selector: #selector(self.plugin.handleTokenChanged(_:)), name: Notification.Name.AppCheckTokenDidChange, object: nil)
1119
}
1220

1321
@objc public func getToken(forceRefresh: Bool, completion: @escaping (String?, Int, Error?) -> Void) {

packages/app-check/ios/Plugin/FirebaseAppCheckPlugin.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import Capacitor
3+
import FirebaseAppCheck
34

45
/**
56
* Please read the Capacitor iOS Plugin Development Guide
@@ -17,10 +18,11 @@ public class FirebaseAppCheckPlugin: CAPPlugin, CAPBridgedPlugin {
1718
public let tag = "FirebaseApp"
1819
public let errorEnabledMissing = "enabled must be provided."
1920
public let errorGetTokenFailed = "Failed to get token."
21+
public let tokenChangedEvent = "tokenChanged"
2022
private var implementation: FirebaseAppCheck?
2123

2224
override public func load() {
23-
implementation = FirebaseAppCheck()
25+
implementation = FirebaseAppCheck(plugin: self)
2426
}
2527

2628
@objc func getToken(_ call: CAPPluginCall) {
@@ -68,4 +70,16 @@ public class FirebaseAppCheckPlugin: CAPPlugin, CAPBridgedPlugin {
6870
implementation?.setTokenAutoRefreshEnabled(enabled)
6971
call.resolve()
7072
}
73+
74+
@objc func handleTokenChanged(_ notification: Notification) {
75+
guard let userInfo = notification.userInfo else {
76+
return
77+
}
78+
79+
let token = userInfo[AppCheckTokenNotificationKey] as? String
80+
81+
var result = JSObject()
82+
result["token"] = token ?? NSNull()
83+
notifyListeners(tokenChangedEvent, data: result, retainUntilConsumed: true)
84+
}
7185
}

packages/app-check/src/definitions.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ export interface FirebaseAppCheckPlugin {
2525
/**
2626
* Called when the App Check token changed.
2727
*
28-
* Only available for Web.
29-
*
3028
* @since 1.3.0
3129
*/
3230
addListener(

0 commit comments

Comments
 (0)