diff --git a/packages/firebase_messaging/CHANGELOG.md b/packages/firebase_messaging/CHANGELOG.md index a79c42f1ec4f..2e25f0e926c9 100644 --- a/packages/firebase_messaging/CHANGELOG.md +++ b/packages/firebase_messaging/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.0.17 + +* Update README to work with background notification for Android + ## 6.0.16 * Fix push notifications clearing after app launch on iOS. diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 93510e03a4a8..3327731868bd 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -73,34 +73,55 @@ By default background messaging is not enabled. To handle messages in the backgr 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 `/android/app/src/main/java//`. +1. Add an `FirebaseCloudMessagingPluginRegistrant.kt` class to your app in the same directory as your `MainActivity.kt`. This is typically found in `/android/app/src/main/kotlin//`. + + ```kt + import io.flutter.plugin.common.PluginRegistry + import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin + + + object FirebaseCloudMessagingPluginRegistrant { + fun registerWith(registry: PluginRegistry) { + if (alreadyRegisteredWith(registry)) { + return + } + FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")) + } + + private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean { + val key = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName + if (registry.hasPlugin(key)) { + return true + } + registry.registrarFor(key) + return false + } + } + ``` +1. Add an `Application.kt` class to your app in the same directory as your `MainActivity.kt`. This is typically found in `/android/app/src/main/kotlin//`. - ```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); - } - } - ``` + ```kt + import io.flutter.app.FlutterApplication + import io.flutter.plugin.common.PluginRegistry + import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback + import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService + + + class Application : FlutterApplication(), PluginRegistrantCallback { + override fun onCreate() { + super.onCreate() + FlutterFirebaseMessagingService.setPluginRegistrant(this) + } + + override fun registerWith(registry: PluginRegistry) { + FirebaseCloudMessagingPluginRegistrant.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`. +1. In `Application.kt` and `FirebaseCloudMessagingPluginRegistrant.kt`, 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 + ```kt package com.domain.myapplication; ``` @@ -129,7 +150,7 @@ By default background messaging is not enabled. To handle messages in the backgr ``` Note: the protocol of `data` and `notification` are in line with the - fields defined by a [RemoteMessage](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage). + fields defined by a [RemoteMessage](https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/RemoteMessage). You may also need to force stop or uninstall & reinstall the app to make the background notifications work. 1. Set `onBackgroundMessage` handler when calling `configure`