I am trying to use Firebase Messaging in a Flutter app. I follow all the instruction from the package.
In my pubspec.yaml I have these packages:
dependencies:
flutter:
sdk: flutter
...
firebase_messaging: ^5.1.5
...
My main.app is just it:
void main() {
runApp(MyApp());
}
MyApp is a StatefullWidget and inside the _MyAppState I have:
FirebaseMessaging firebaseMessaging = FirebaseMessaging();
@override
void initState() {
super.initState();
NotificationHandler().initializeFcmNotification();
}
All the code is inside whe notification_handler.dart (thanks to @HarishPenta):
...
import 'package:firebase_messaging/firebase_messaging.dart';
...
class NotificationHandler {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
FirebaseMessaging _fcm = FirebaseMessaging();
StreamSubscription iosSubscription;
static final NotificationHandler _singleton =
new NotificationHandler._internal();
factory NotificationHandler() {
return _singleton;
}
NotificationHandler._internal();
Future<dynamic> myBackgroundMessageHandler(
Map<String, dynamic> message) async {
print("onLaunch: $message");
// Or do other work.
}
initializeFcmNotification() async {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
new AndroidInitializationSettings('ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
// save the token OR subscribe to a topic here
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
//onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}
...}
In my AndroidManifest.xml I have:
<activity
android:name=".MainActivity"
...
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
and, finally, inside the app/src/main/java//Application.java I have:
package <my ID app>; --> the same ID in google-services.json
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistry.PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
Well, the app start OK, and, in foreground, when I send a notification, the app stop and I can see this error message:
D/AndroidRuntime(26933): Shutting down VM
E/AndroidRuntime(26933): FATAL EXCEPTION: main
E/AndroidRuntime(26933): Process: br.com.htsnet.emergencia, PID: 26933
E/AndroidRuntime(26933): java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(26933): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3167)
E/AndroidRuntime(26933): at android.app.ActivityThread.access$1900(ActivityThread.java:177)
E/AndroidRuntime(26933): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
E/AndroidRuntime(26933): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(26933): at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime(26933): at android.app.ActivityThread.main(ActivityThread.java:5951)
E/AndroidRuntime(26933): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(26933): at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(26933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
E/AndroidRuntime(26933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
E/AndroidRuntime(26933): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(26933): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/AndroidRuntime(26933): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
E/AndroidRuntime(26933): at android.app.ActivityThread.handleCreateService(ActivityThread.java:3157)
E/AndroidRuntime(26933): ... 9 more
I am using Android Studio 3.5, Firebase Functions to send notification whith data in paypload (click_action: 'FLUTTER_NOTIFICATION_CLICK').
Add the below mentioned files in your android/app/main/java/com/
Application.java
package com.example.<application Name>;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
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);
}
}
FirebaseCloudMessagingPluginRegistrant
package com.example.<application Name>;
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;
}
}
MainActivity
package com.example.<application Name>;
import android.os.Bundle;
import `io.flutter.app.FlutterActivity`;
import `io.flutter.plugins.GeneratedPluginRegistrant`;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With