From 89bed5df5b8012f5e9296f5da0adf3746d8b171a Mon Sep 17 00:00:00 2001 From: supertiger1234 Date: Fri, 31 Jul 2020 18:36:50 +0100 Subject: [PATCH 1/5] firebase_messaging: Getting Started Updated getting started instructions to fix background notifications. --- packages/firebase_messaging/README.md | 72 +++++++++++++++++++-------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 93510e03a4a8..108f7d93f365 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -16,7 +16,10 @@ To use this plugin, add `firebase_messaging` as a [dependency in your pubspec.ya Check out the `example` directory for a sample app using Firebase Cloud Messaging. ### Android Integration - +Make sure you have created your Flutter app with the java option: +``` +flutter create -i objc -a java appname +``` To integrate your plugin into the Android part of your app, follow these steps: 1. Using the [Firebase Console](https://console.firebase.google.com/) add an Android app to your project: Follow the assistant, download the generated `google-services.json` file and place it inside `android/app`. @@ -73,32 +76,59 @@ 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 `FirebaseCloudMessagingPluginRegistrant.java` class to your app in the same directory as your `MainActivity.java`. This is typically found in `/android/app/src/main/java//`. + + ```java + package io.flutter.plugins.firebasemessagingexample; + + import io.flutter.plugin.common.PluginRegistry; + import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin; + + public final class FirebaseCloudMessagingPluginRegistrant{ + public static void registerWith(PluginRegistry registry) { + if (alreadyRegisteredWith(registry)) { + return; + } + FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")); + } + + private static boolean alreadyRegisteredWith(PluginRegistry registry) { + final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName(); + if (registry.hasPlugin(key)) { + return true; + } + registry.registrarFor(key); + return false; + } + } + ``` 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//`. ```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; + package io.flutter.plugins.firebasemessagingexample; - 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); - } - } + 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.FirebaseMessagingPlugin; + 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) { + 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.java` and `FirebaseCloudMessagingPluginRegistrant.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; From 181e3e6d4d9793495018cb159b8072ebef5d94c8 Mon Sep 17 00:00:00 2001 From: supertiger1234 Date: Fri, 31 Jul 2020 18:42:45 +0100 Subject: [PATCH 2/5] fixed spacing --- packages/firebase_messaging/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 108f7d93f365..871e9e593d5f 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -104,9 +104,9 @@ By default background messaging is not enabled. To handle messages in the backgr ``` 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//`. - ```java + ```java package io.flutter.plugins.firebasemessagingexample; - + import io.flutter.app.FlutterApplication; import io.flutter.plugin.common.PluginRegistry; import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback; @@ -126,7 +126,7 @@ By default background messaging is not enabled. To handle messages in the backgr FirebaseCloudMessagingPluginRegistrant.registerWith(registry); } } - ``` + ``` 1. In `Application.java` and `FirebaseCloudMessagingPluginRegistrant.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`. From d930ad6952f20e3aaae65a6ad3877e96a3b2cbf6 Mon Sep 17 00:00:00 2001 From: supertiger1234 Date: Fri, 31 Jul 2020 19:10:21 +0100 Subject: [PATCH 3/5] updated readme --- packages/firebase_messaging/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) 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. From daabf0a28a6d260679581958496dcdaeda7daba4 Mon Sep 17 00:00:00 2001 From: supertiger1234 Date: Sat, 1 Aug 2020 14:05:43 +0100 Subject: [PATCH 4/5] Changed code to kotlin --- packages/firebase_messaging/README.md | 77 ++++++++++++--------------- 1 file changed, 34 insertions(+), 43 deletions(-) diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 871e9e593d5f..9eb4fabcd35b 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -16,10 +16,7 @@ To use this plugin, add `firebase_messaging` as a [dependency in your pubspec.ya Check out the `example` directory for a sample app using Firebase Cloud Messaging. ### Android Integration -Make sure you have created your Flutter app with the java option: -``` -flutter create -i objc -a java appname -``` + To integrate your plugin into the Android part of your app, follow these steps: 1. Using the [Firebase Console](https://console.firebase.google.com/) add an Android app to your project: Follow the assistant, download the generated `google-services.json` file and place it inside `android/app`. @@ -76,61 +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 `FirebaseCloudMessagingPluginRegistrant.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//`. - ```java - package io.flutter.plugins.firebasemessagingexample; + ```kt + import io.flutter.plugin.common.PluginRegistry + import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin - import io.flutter.plugin.common.PluginRegistry; - import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin; - public final class FirebaseCloudMessagingPluginRegistrant{ - public static void registerWith(PluginRegistry registry) { + object FirebaseCloudMessagingPluginRegistrant { + fun registerWith(registry: PluginRegistry) { if (alreadyRegisteredWith(registry)) { - return; + return } - FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")); + FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")) } - private static boolean alreadyRegisteredWith(PluginRegistry registry) { - final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName(); + private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean { + val key = FirebaseCloudMessagingPluginRegistrant::class.java.canonicalName if (registry.hasPlugin(key)) { - return true; + return true } - registry.registrarFor(key); - return false; + registry.registrarFor(key) + return false } } ``` -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//`. - - ```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.FirebaseMessagingPlugin; - 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) { - FirebaseCloudMessagingPluginRegistrant.registerWith(registry); - } +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//`. + + ```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` and `FirebaseCloudMessagingPluginRegistrant.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; ``` From 1651f2acb494ce394e9745dfea871aeec67d4ae6 Mon Sep 17 00:00:00 2001 From: supertiger1234 Date: Sat, 1 Aug 2020 14:12:56 +0100 Subject: [PATCH 5/5] added a notice --- packages/firebase_messaging/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 9eb4fabcd35b..3327731868bd 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -150,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`