Skip to content

[firebase_messaging] Android: background service refactoring #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 8.0.0

* Refactors the Android portion for background messages. It is now using a
`FlutterEngine` internally, which does removes the requirement to manually
register a plugin registrant or include FCM dependencies in the host App.

## 7.0.0

* Depend on `firebase_core` and migrate plugin to use `firebase_core` native SDK versioning features;
Expand Down
49 changes: 0 additions & 49 deletions packages/firebase_messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,55 +61,6 @@ for more.

By default background messaging is not enabled. To handle messages in the background:

1. Add the `com.google.firebase:firebase-messaging` dependency in your app-level `build.gradle` file that is typically located at `<app-name>/android/app/build.gradle`.

```gradle
dependencies {
// ...

implementation 'com.google.firebase:firebase-messaging:<latest_version>'
}
```

Note: you can find out what the latest version of the plugin is [here ("Cloud Messaging")](https://firebase.google.com/support/release-notes/android#latest_sdk_versions).

1. Add an `Application.java` class to your app in the same directory as your `MainActivity.java`. This is typically found in `<app-name>/android/app/src/main/java/<app-organization-path>/`.

```java
package io.flutter.plugins.firebasemessagingexample;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}

@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
```

1. In `Application.java`, make sure to change `package io.flutter.plugins.firebasemessagingexample;` to your package's identifier. Your package's identifier should be something like `com.domain.myapplication`.

```java
package com.domain.myapplication;
```

1. Set name property of application in `AndroidManifest.xml`. This is typically found in `<app-name>/android/app/src/main/`.

```xml
<application android:name=".Application" ...>
```

1. Define a **TOP-LEVEL** or **STATIC** function to handle background messages

```dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.NewIntentListener;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.plugin.common.PluginRegistry.ViewDestroyListener;
import io.flutter.view.FlutterNativeView;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -41,31 +43,33 @@ public class FirebaseMessagingPlugin extends BroadcastReceiver
private static final String TAG = "FirebaseMessagingPlugin";

private MethodChannel channel;
private Context applicationContext;
private Activity mainActivity;

public static void registerWith(Registrar registrar) {
FirebaseMessagingPlugin instance = new FirebaseMessagingPlugin();
public static void registerWith(final Registrar registrar) {
final FirebaseMessagingPlugin instance = new FirebaseMessagingPlugin();
instance.setActivity(registrar.activity());
registrar.addNewIntentListener(instance);
instance.onAttachedToEngine(registrar.context(), registrar.messenger());
registrar.addViewDestroyListener(
new ViewDestroyListener() {
@Override
public boolean onViewDestroy(FlutterNativeView view) {
instance.onDetachedFromEngine(registrar.context());
return false;
}
});
}

private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) {
this.applicationContext = context;
channel = new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_messaging");
final MethodChannel backgroundCallbackChannel =
new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_messaging_background");

channel.setMethodCallHandler(this);
backgroundCallbackChannel.setMethodCallHandler(this);
FlutterFirebaseMessagingService.setBackgroundChannel(backgroundCallbackChannel);

// Register broadcast receiver
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(FlutterFirebaseMessagingService.ACTION_TOKEN);
intentFilter.addAction(FlutterFirebaseMessagingService.ACTION_REMOTE_MESSAGE);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(applicationContext);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(context);
manager.registerReceiver(this, intentFilter);
}

Expand All @@ -80,7 +84,11 @@ public void onAttachedToEngine(FlutterPluginBinding binding) {

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
LocalBroadcastManager.getInstance(binding.getApplicationContext()).unregisterReceiver(this);
onDetachedFromEngine(binding.getApplicationContext());
}

private void onDetachedFromEngine(Context applicationContext) {
LocalBroadcastManager.getInstance(applicationContext).unregisterReceiver(this);
}

@Override
Expand Down Expand Up @@ -149,14 +157,10 @@ public void onMethodCall(final MethodCall call, final Result result) {
/* Even when the app is not active the `FirebaseMessagingService` extended by
* `FlutterFirebaseMessagingService` allows incoming FCM messages to be handled.
*
* `FcmDartService#start` and `FcmDartService#initialized` are the two methods used
* to optionally setup handling messages received while the app is not active.
* `FcmDartService#start` is setup to handle messages received while the app is not active.
*
* `FcmDartService#start` sets up the plumbing that allows messages received while
* the app is not active to be handled by a background isolate.
*
* `FcmDartService#initialized` is called by the Dart side when the plumbing for
* background message handling is complete.
*/
if ("FcmDartService#start".equals(call.method)) {
long setupCallbackHandle = 0;
Expand All @@ -171,12 +175,10 @@ public void onMethodCall(final MethodCall call, final Result result) {
e.printStackTrace();
}
FlutterFirebaseMessagingService.setBackgroundSetupHandle(mainActivity, setupCallbackHandle);
FlutterFirebaseMessagingService.startBackgroundIsolate(mainActivity, setupCallbackHandle);
FlutterFirebaseMessagingService.setBackgroundMessageHandle(
mainActivity, backgroundMessageHandle);
result.success(true);
} else if ("FcmDartService#initialized".equals(call.method)) {
FlutterFirebaseMessagingService.onInitialized();
mainActivity.startService(new Intent(mainActivity, FlutterFirebaseMessagingService.class));

result.success(true);
} else if ("configure".equals(call.method)) {
FirebaseInstanceId.getInstance()
Expand Down
Loading