Skip to content

Commit

Permalink
Make fcmSetupBackgroundChannel top-level
Browse files Browse the repository at this point in the history
  • Loading branch information
kroikie committed Sep 5, 2019
1 parent 37d4156 commit 44c501f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
76 changes: 38 additions & 38 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,44 @@ import 'package:platform/platform.dart';

typedef Future<dynamic> MessageHandler(Map<String, dynamic> message);

/// Setup method channel to handle Firebase Cloud Messages received while
/// the Flutter app is not active. The handle for this method is generated
/// and passed to the Android side so that the background isolate knows where
/// to send background messages for processing.
///
/// Your app should never call this method directly, this is only for use
/// by the firebase_messaging plugin to setup background message handling.
@visibleForTesting
void fcmSetupBackgroundChannel(
{MethodChannel backgroundChannel = const MethodChannel(
'plugins.flutter.io/firebase_messaging_background')}) async {
// Setup Flutter state needed for MethodChannels.
WidgetsFlutterBinding.ensureInitialized();

// This is where the magic happens and we handle background events from the
// native portion of the plugin.
backgroundChannel.setMethodCallHandler((MethodCall call) async {
if (call.method == 'handleBackgroundMessage') {
final CallbackHandle handle =
CallbackHandle.fromRawHandle(call.arguments['handle']);
final Function handlerFunction =
PluginUtilities.getCallbackFromHandle(handle);
try {
await handlerFunction(
Map<String, dynamic>.from(call.arguments['message']));
} catch (e) {
print('Unable to handle incoming background message.');
print(e);
}
return Future<void>.value();
}
});

// Once we've finished initializing, let the native portion of the plugin
// know that it can start scheduling handling messages.
backgroundChannel.invokeMethod<void>('FcmDartService#initialized');
}

/// Implementation of the Firebase Cloud Messaging API for Flutter.
///
/// Your app should call [requestNotificationPermissions] first and then
Expand All @@ -29,44 +67,6 @@ class FirebaseMessaging {
const MethodChannel('plugins.flutter.io/firebase_messaging'),
const LocalPlatform());

/// Setup method channel to handle Firebase Cloud Messages received while
/// the Flutter app is not active. The handle for this method is generated
/// and passed to the Android side so that the background isolate knows where
/// to send background messages for processing.
///
/// Your app should never call this method directly, this is only for use
/// by the firebase_messaging plugin to setup background message handling.
@visibleForTesting
static void fcmSetupBackgroundChannel(
{MethodChannel backgroundChannel = const MethodChannel(
'plugins.flutter.io/firebase_messaging_background')}) async {
// Setup Flutter state needed for MethodChannels.
WidgetsFlutterBinding.ensureInitialized();

// This is where the magic happens and we handle background events from the
// native portion of the plugin.
backgroundChannel.setMethodCallHandler((MethodCall call) async {
if (call.method == 'handleBackgroundMessage') {
final CallbackHandle handle =
CallbackHandle.fromRawHandle(call.arguments['handle']);
final Function handlerFunction =
PluginUtilities.getCallbackFromHandle(handle);
try {
await handlerFunction(
Map<String, dynamic>.from(call.arguments['message']));
} catch (e) {
print('Unable to handle incoming background message.');
print(e);
}
return Future<void>.value();
}
});

// Once we've finished initializing, let the native portion of the plugin
// know that it can start scheduling handling messages.
backgroundChannel.invokeMethod<void>('FcmDartService#initialized');
}

final MethodChannel _channel;
final Platform _platform;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void main() {
});

test('setupBackgroundCallback', () {
FirebaseMessaging.fcmSetupBackgroundChannel(
fcmSetupBackgroundChannel(
backgroundChannel: mockBackgroundChannel);
verify(
mockBackgroundChannel.invokeMethod<void>('FcmDartService#initialized'));
Expand Down

0 comments on commit 44c501f

Please sign in to comment.