Skip to content

Commit

Permalink
fix(messaging): Method channel handlers were not created if `Firebase…
Browse files Browse the repository at this point in the history
…Messaging.instance.*` is not invoked beforehand (firebase#11447)

Co-authored-by: Guillaume Bernos <guillaume@bernos.dev>
  • Loading branch information
2 people authored and khanliKU committed Aug 18, 2023
1 parent 770bbbe commit 4f72db7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
Expand Up @@ -68,12 +68,31 @@ void _firebaseMessagingCallbackDispatcher() {
class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {
/// Create an instance of [MethodChannelFirebaseMessaging] with optional [FirebaseApp]
MethodChannelFirebaseMessaging({required FirebaseApp app})
: super(appInstance: app) {
if (_initialized) return;
channel.setMethodCallHandler((MethodCall call) async {
: super(appInstance: app);

late bool _autoInitEnabled;

static bool _bgHandlerInitialized = false;

/// Returns a stub instance to allow the platform interface to access
/// the class instance statically.
static MethodChannelFirebaseMessaging get instance {
return MethodChannelFirebaseMessaging._();
}

/// Internal stub class initializer.
///
/// When the user code calls an auth method, the real instance is
/// then initialized via the [delegateFor] method.
MethodChannelFirebaseMessaging._() : super(appInstance: null);

static void setMethodCallHandlers() {
MethodChannelFirebaseMessaging.channel
.setMethodCallHandler((MethodCall call) async {
switch (call.method) {
case 'Messaging#onTokenRefresh':
_tokenStreamController.add(call.arguments as String);
MethodChannelFirebaseMessaging.tokenStreamController
.add(call.arguments as String);
break;
case 'Messaging#onMessage':
Map<String, dynamic> messageMap =
Expand All @@ -97,33 +116,14 @@ class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {
throw UnimplementedError('${call.method} has not been implemented');
}
});
_initialized = true;
}

late bool _autoInitEnabled;

static bool _initialized = false;
static bool _bgHandlerInitialized = false;

/// Returns a stub instance to allow the platform interface to access
/// the class instance statically.
static MethodChannelFirebaseMessaging get instance {
return MethodChannelFirebaseMessaging._();
}

/// Internal stub class initializer.
///
/// When the user code calls an auth method, the real instance is
/// then initialized via the [delegateFor] method.
MethodChannelFirebaseMessaging._() : super(appInstance: null);

/// The [MethodChannel] to which calls will be delegated.
@visibleForTesting
static const MethodChannel channel = MethodChannel(
'plugins.flutter.io/firebase_messaging',
);

final StreamController<String> _tokenStreamController =
// ignore: close_sinks, never closed
static StreamController<String> tokenStreamController =
StreamController<String>.broadcast();

@override
Expand Down Expand Up @@ -305,7 +305,7 @@ class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {

@override
Stream<String> get onTokenRefresh {
return _tokenStreamController.stream;
return tokenStreamController.stream;
}

@override
Expand Down
Expand Up @@ -7,6 +7,7 @@ import 'dart:async';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging_platform_interface/firebase_messaging_platform_interface.dart';
import 'package:flutter/foundation.dart';
import 'package:meta/meta.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

Expand Down Expand Up @@ -51,6 +52,10 @@ abstract class FirebaseMessagingPlatform extends PlatformInterface {
/// It will always default to [MethodChannelFirebaseMessaging]
/// if no other implementation was provided.
static FirebaseMessagingPlatform get instance {
if (_instance == null) {
// This is only called for method channels since Web is setting the instance before we use `get`
MethodChannelFirebaseMessaging.setMethodCallHandlers();
}
return _instance ??= MethodChannelFirebaseMessaging.instance;
}

Expand Down

0 comments on commit 4f72db7

Please sign in to comment.