A demo Flutter application for Firebase Cloud Messaging (Firebse push notification0
Clone this repository locally. The folder structure is somewhat based on clean code architecture
How to implement firebase in you project:
-
Create flutter project
-
Create project in firebase console.
To get sh1 key go to java/jdk/bin & run this cmnd in cmd:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey storepass android -keypass android
or check this post for other easier way to get SH1 Key
-
In android/app -> add
google-service.json
(Get this file from firebase console -To get this file register your app in firebase console here) -
In
pubspec.yaml
add,
flutter_test: sdk: flutter firebase_messaging: ^6.0.13 firebase_analytics: ^5.0.11 #optional
-
add Application.kt in android/app/src/main/kotlin/com/yourdomain/appname/
Application.kt
(ignore the error)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 //import com.google.firebase.messaging.FirebaseMessagingService class Application : FlutterApplication() , PluginRegistrantCallback { override fun onCreate() { super.onCreate(); FlutterFirebaseMessagingService.setPluginRegistrant(this); } override fun registerWith( registry: PluginRegistry) { GeneratedPluginRegistrant.registerWith(registry); } }
if error occurred, replace
GeneratedPluginRegistrant.registerWith(registry);
with,
registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
-
In src/main/manifest, in
<application/>
tagreplace/add,
android:name=".Application"
Inside
<activity/>
tag (below existing intent filter tag)add,
<activity <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>
-
In
app/build.gradle
add,
implementation 'com.google.firebase:firebase-messaging:20.1.5'
apply plugin: 'com.google.gms.google-services'
addd this at the bottom -
In
android/build.gradle
add,
classpath 'com.google.gms:google-services:4.3.3'
-
To get token and suscribe to a topic use following code in your
main.dart
:main.dart
import 'package:firebase_messaging/firebase_messaging.dart'; final FirebaseMessaging _messaging = FirebaseMessaging(); @override void initState() { super.initState(); _messaging.subscribeToTopic("general"); _messaging.getToken().then((token) { print(token); }); }
-
run flutter clean/ re-install app/ invalidate cache and restart android studio
-
enjoy.
For more help Read official documentation Here