Skip to content

Commit 285a7ad

Browse files
committed
feat: registerIdChangeListener Pigeons
1 parent 1ace3ec commit 285a7ad

File tree

9 files changed

+156
-45
lines changed

9 files changed

+156
-45
lines changed

packages/firebase_app_installations/firebase_app_installations/android/src/main/java/io/flutter/plugins/firebase/installations/GeneratedAndroidFirebaseAppInstallations.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,10 @@ void onIdChange(
367367
@NonNull String newId,
368368
@NonNull VoidResult result);
369369

370+
void registerIdChangeListener(
371+
@NonNull AppInstallationsPigeonFirebaseApp app,
372+
@NonNull Result<String> result);
373+
370374
/** The codec used by FirebaseAppInstallationsHostApi. */
371375
static @NonNull MessageCodec<Object> getCodec() {
372376
return PigeonCodec.INSTANCE;
@@ -555,6 +559,39 @@ public void error(Throwable error) {
555559
channel.setMessageHandler(null);
556560
}
557561
}
562+
{
563+
BasicMessageChannel<Object> channel =
564+
new BasicMessageChannel<>(
565+
binaryMessenger,
566+
"dev.flutter.pigeon.firebase_app_installations_platform_interface.FirebaseAppInstallationsHostApi.registerIdChangeListener"
567+
+ messageChannelSuffix,
568+
getCodec());
569+
if (api != null) {
570+
channel.setMessageHandler(
571+
(message, reply) -> {
572+
ArrayList<Object> wrapped = new ArrayList<>();
573+
ArrayList<Object> args = (ArrayList<Object>) message;
574+
AppInstallationsPigeonFirebaseApp appArg =
575+
(AppInstallationsPigeonFirebaseApp) args.get(0);
576+
Result<String> resultCallback =
577+
new Result<String>() {
578+
public void success(String result) {
579+
wrapped.add(0, result);
580+
reply.reply(wrapped);
581+
}
582+
583+
public void error(Throwable error) {
584+
ArrayList<Object> wrappedError = wrapError(error);
585+
reply.reply(wrappedError);
586+
}
587+
};
588+
589+
api.registerIdChangeListener(appArg, resultCallback);
590+
});
591+
} else {
592+
channel.setMessageHandler(null);
593+
}
594+
}
558595
}
559596
}
560597
/** Generated class from Pigeon that represents Flutter messages that can be called from Java. */

packages/firebase_app_installations/firebase_app_installations/android/src/main/java/io/flutter/plugins/firebase/installations/firebase_app_installations/FirebaseInstallationsPlugin.java

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -60,47 +60,12 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
6060
removeEventListeners();
6161
}
6262

63-
private FirebaseInstallations getInstallations(Map<String, Object> arguments) {
64-
@NonNull String appName = (String) Objects.requireNonNull(arguments.get("appName"));
65-
FirebaseApp app = FirebaseApp.getInstance(appName);
66-
return FirebaseInstallations.getInstance(app);
67-
}
68-
6963
private FirebaseInstallations getInstallations(AppInstallationsPigeonFirebaseApp appArg) {
7064
@NonNull String appName = appArg.getAppName();
7165
FirebaseApp app = FirebaseApp.getInstance(appName);
7266
return FirebaseInstallations.getInstance(app);
7367
}
7468

75-
private Task<String> registerIdChangeListener(Map<String, Object> arguments) {
76-
TaskCompletionSource<String> taskCompletionSource = new TaskCompletionSource<>();
77-
78-
cachedThreadPool.execute(
79-
() -> {
80-
try {
81-
String appName = (String) Objects.requireNonNull(arguments.get("appName"));
82-
FirebaseInstallations firebaseInstallations = getInstallations(arguments);
83-
84-
io.flutter.plugins.firebase.installations.firebase_app_installations
85-
.TokenChannelStreamHandler
86-
handler =
87-
new io.flutter.plugins.firebase.installations.firebase_app_installations
88-
.TokenChannelStreamHandler(firebaseInstallations);
89-
90-
final String name = METHOD_CHANNEL_NAME + "/token/" + appName;
91-
final EventChannel channel = new EventChannel(messenger, name);
92-
channel.setStreamHandler(handler);
93-
streamHandlers.put(channel, handler);
94-
95-
taskCompletionSource.setResult(name);
96-
} catch (Exception e) {
97-
taskCompletionSource.setException(e);
98-
}
99-
});
100-
101-
return taskCompletionSource.getTask();
102-
}
103-
10469
// Pigeon FirebaseAppInstallationsHostApi implementation.
10570

10671
@Override
@@ -188,6 +153,36 @@ public void onIdChange(
188153
result.success();
189154
}
190155

156+
@Override
157+
public void registerIdChangeListener(
158+
@NonNull AppInstallationsPigeonFirebaseApp app,
159+
@NonNull GeneratedAndroidFirebaseAppInstallations.Result<String> result) {
160+
cachedThreadPool.execute(
161+
() -> {
162+
try {
163+
FirebaseInstallations firebaseInstallations = getInstallations(app);
164+
@NonNull String appName = app.getAppName();
165+
166+
io.flutter.plugins.firebase.installations.firebase_app_installations
167+
.TokenChannelStreamHandler
168+
handler =
169+
new io.flutter.plugins.firebase.installations.firebase_app_installations
170+
.TokenChannelStreamHandler(firebaseInstallations);
171+
172+
final String name = METHOD_CHANNEL_NAME + "/token/" + appName;
173+
final EventChannel channel = new EventChannel(messenger, name);
174+
channel.setStreamHandler(handler);
175+
streamHandlers.put(channel, handler);
176+
177+
result.success(name);
178+
} catch (Exception e) {
179+
result.error(
180+
new GeneratedAndroidFirebaseAppInstallations.FlutterError(
181+
"firebase_app_installations", e.getMessage(), getExceptionDetails(e)));
182+
}
183+
});
184+
}
185+
191186
private Map<String, Object> getExceptionDetails(@Nullable Exception exception) {
192187
Map<String, Object> details = new HashMap<>();
193188
details.put("code", "unknown");

packages/firebase_app_installations/firebase_app_installations/ios/firebase_app_installations/Sources/firebase_app_installations/FirebaseAppInstallationsMessages.g.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ protocol FirebaseAppInstallationsHostApi {
267267
completion: @escaping (Result<String, Error>) -> Void)
268268
func onIdChange(app: AppInstallationsPigeonFirebaseApp, newId: String,
269269
completion: @escaping (Result<Void, Error>) -> Void)
270+
func registerIdChangeListener(app: AppInstallationsPigeonFirebaseApp,
271+
completion: @escaping (Result<String, Error>) -> Void)
270272
}
271273

272274
/// Generated setup class from Pigeon to handle messages through the `binaryMessenger`.
@@ -388,6 +390,27 @@ class FirebaseAppInstallationsHostApiSetup {
388390
} else {
389391
onIdChangeChannel.setMessageHandler(nil)
390392
}
393+
let registerIdChangeListenerChannel = FlutterBasicMessageChannel(
394+
name: "dev.flutter.pigeon.firebase_app_installations_platform_interface.FirebaseAppInstallationsHostApi.registerIdChangeListener\(channelSuffix)",
395+
binaryMessenger: binaryMessenger,
396+
codec: codec
397+
)
398+
if let api {
399+
registerIdChangeListenerChannel.setMessageHandler { message, reply in
400+
let args = message as! [Any?]
401+
let appArg = args[0] as! AppInstallationsPigeonFirebaseApp
402+
api.registerIdChangeListener(app: appArg) { result in
403+
switch result {
404+
case let .success(res):
405+
reply(wrapResult(res))
406+
case let .failure(error):
407+
reply(wrapError(error))
408+
}
409+
}
410+
}
411+
} else {
412+
registerIdChangeListenerChannel.setMessageHandler(nil)
413+
}
391414
}
392415
}
393416

packages/firebase_app_installations/firebase_app_installations/ios/firebase_app_installations/Sources/firebase_app_installations/FirebaseInstallationsPlugin.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ public class FirebaseInstallationsPlugin: NSObject, FLTFirebasePluginProtocol,
198198
completion(.success(()))
199199
}
200200

201+
func registerIdChangeListener(app: AppInstallationsPigeonFirebaseApp,
202+
completion: @escaping (Result<String, Error>) -> Void) {
203+
let instance = getInstallations(app: app)
204+
let appName = app.appName
205+
let eventChannelName = kFLTFirebaseInstallationsChannelName + "/token/" + appName
206+
207+
let eventChannel = FlutterEventChannel(name: eventChannelName, binaryMessenger: messenger)
208+
209+
if streamHandler[eventChannelName] == nil {
210+
streamHandler[eventChannelName] = IdChangedStreamHandler(instance: instance)
211+
}
212+
213+
eventChannel.setStreamHandler(streamHandler[eventChannelName]!)
214+
215+
completion(.success(eventChannelName))
216+
}
217+
201218
/// Registers a listener for changes in the Installations Id.
202219
/// - Parameter arguments: the arguments passed by the Dart calling method
203220
/// - Parameter result: the result instance used to send the result to Dart.

packages/firebase_app_installations/firebase_app_installations/windows/messages.g.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ EncodableValue FirebaseAppInstallationsHostApi::WrapError(
425425
error.details()});
426426
}
427427

428+
// Note: The concrete Windows plugin does not yet implement RegisterIdChangeListener;
429+
// this stub keeps the generated HostApi interface in sync with Dart/iOS/Android.
430+
428431
// Generated class from Pigeon that represents Flutter messages that can be
429432
// called from C++.
430433
FirebaseAppInstallationsFlutterApi::FirebaseAppInstallationsFlutterApi(

packages/firebase_app_installations/firebase_app_installations/windows/messages.g.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ class FirebaseAppInstallationsHostApi {
153153
virtual void OnIdChange(
154154
const AppInstallationsPigeonFirebaseApp& app, const std::string& new_id,
155155
std::function<void(std::optional<FlutterError> reply)> result) = 0;
156+
virtual void RegisterIdChangeListener(
157+
const AppInstallationsPigeonFirebaseApp& app,
158+
std::function<void(ErrorOr<std::string> reply)> result) = 0;
156159

157160
// The codec used by FirebaseAppInstallationsHostApi.
158161
static const flutter::StandardMessageCodec& GetCodec();

packages/firebase_app_installations/firebase_app_installations_platform_interface/lib/src/method_channel/method_channel_firebase_app_installations.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ class MethodChannelFirebaseAppInstallations
2323
return MethodChannelFirebaseAppInstallations._();
2424
}
2525

26-
/// The [MethodChannelFirebaseFunctions] method channel.
27-
static const MethodChannel channel = MethodChannel(
28-
'plugins.flutter.io/firebase_app_installations',
29-
);
30-
3126
static final Map<String, StreamController<String>> _idTokenChangesListeners =
3227
<String, StreamController<String>>{};
3328

@@ -37,11 +32,12 @@ class MethodChannelFirebaseAppInstallations
3732
final controller = _idTokenChangesListeners[app.name] =
3833
StreamController<String>.broadcast();
3934

40-
channel.invokeMethod<String>(
41-
'FirebaseInstallations#registerIdChangeListener', {
42-
'appName': app.name,
43-
}).then((channelName) {
44-
final events = EventChannel(channelName!, channel.codec);
35+
_api
36+
.registerIdChangeListener(
37+
AppInstallationsPigeonFirebaseApp(appName: app.name),
38+
)
39+
.then((channelName) {
40+
final events = EventChannel(channelName);
4541

4642
events
4743
.receiveGuardedBroadcastStream(onError: convertPlatformException)

packages/firebase_app_installations/firebase_app_installations_platform_interface/lib/src/pigeon/messages.pigeon.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,38 @@ class FirebaseAppInstallationsHostApi {
305305
}
306306
}
307307

308+
Future<String> registerIdChangeListener(
309+
AppInstallationsPigeonFirebaseApp app) async {
310+
final String pigeonVar_channelName =
311+
'dev.flutter.pigeon.firebase_app_installations_platform_interface.FirebaseAppInstallationsHostApi.registerIdChangeListener$pigeonVar_messageChannelSuffix';
312+
final BasicMessageChannel<Object?> pigeonVar_channel =
313+
BasicMessageChannel<Object?>(
314+
pigeonVar_channelName,
315+
pigeonChannelCodec,
316+
binaryMessenger: pigeonVar_binaryMessenger,
317+
);
318+
final Future<Object?> pigeonVar_sendFuture =
319+
pigeonVar_channel.send(<Object?>[app]);
320+
final List<Object?>? pigeonVar_replyList =
321+
await pigeonVar_sendFuture as List<Object?>?;
322+
if (pigeonVar_replyList == null) {
323+
throw _createConnectionError(pigeonVar_channelName);
324+
} else if (pigeonVar_replyList.length > 1) {
325+
throw PlatformException(
326+
code: pigeonVar_replyList[0]! as String,
327+
message: pigeonVar_replyList[1] as String?,
328+
details: pigeonVar_replyList[2],
329+
);
330+
} else if (pigeonVar_replyList[0] == null) {
331+
throw PlatformException(
332+
code: 'null-error',
333+
message: 'Host platform returned null value for non-null return value.',
334+
);
335+
} else {
336+
return (pigeonVar_replyList[0] as String?)!;
337+
}
338+
}
339+
308340
Future<void> onIdChange(
309341
AppInstallationsPigeonFirebaseApp app, String newId) async {
310342
final String pigeonVar_channelName =

packages/firebase_app_installations/firebase_app_installations_platform_interface/pigeons/messages.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ abstract class FirebaseAppInstallationsHostApi {
6666

6767
@async
6868
void onIdChange(AppInstallationsPigeonFirebaseApp app, String newId);
69+
70+
/// Registers an ID change listener on the host and returns the EventChannel name
71+
/// used to stream token changes for the given app.
72+
@async
73+
String registerIdChangeListener(AppInstallationsPigeonFirebaseApp app);
6974
}
7075

7176
@FlutterApi()

0 commit comments

Comments
 (0)