How to check if my flutter app is properly set up with firebase_messaging push notifications

Viewed 496

I need to make an Android app where the user can receive notifications whenever I send them in the Firebase Cloud Messaging console. I am building it with Flutter.

I have gone through all the setup and I think the app is correctly connecting to Firebase, because there are no error logs.

However, when I send messages via Firebase, my app (which is running on local emulator) doesn't show any notifications.

This is the code of my app.

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:joves_lectors/webview_container.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  Future<void> initFirebaseMessaging() async {
    print(await FirebaseMessaging.instance.getInitialMessage());
    FirebaseMessaging.onMessage.listen((message) {
      if(message.notification != null) {
        print(message.notification.body);
        print(message.notification.title);
      }
    });
  }

  @override
  void initState() {
    super.initState();
    print(FirebaseMessaging.instance.pluginConstants);
    print(FirebaseMessaging.instance.isSupported());
    initFirebaseMessaging();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Text("Hello")//WebViewContainer("https://jornades.joveslectors.cat"),
    );
  }
}

If I go to the Firebase console and send a Cloud Message notification, nothing happens. I've been trying to debug the problem, but I don't know how I can check that my app is connected to Firebase Messaging. I don't get any error messages. This is what my console prints:

I/flutter ( 7980): {AUTO_INIT_ENABLED: true}
I/flutter ( 7980): true
I/flutter ( 7980): null

I don't know if I am missing any steps or making any mistake with my code.

These are my dependencies:

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  webview_flutter: ^2.0.9
  firebase_core: ^1.3.0
  firebase_messaging: ^10.0.2
  flutter_local_notifications: ^6.0.0
1 Answers

i) So you must have to add this below code just before this tag </activity> in your android/app/src/main/androidManifest.xml file

<intent-filter>
    <action android:name="FLUTTER_NOTIFICATION_CLICK" />
    <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

ii) Also add this meta data just before the </application> tag

    <meta-data 
android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="default_notification_channel_id" />

iii) so when you sends the push notification you must pass the FLUTTER_NOTIFICATION_CLICK in your body in json request like the below request

{
"to":"-----YOUR SERVER KEY -------",
 "notification" : {
  "sound" : "default",
  "body" :  "test body",
  "title" : "test title",
  "content_available" : true,
  "priority" : "high"
 },
 "data" : {
  "click_action": "FLUTTER_NOTIFICATION_CLICK",
  "sound" : "default",
  "body" :  "test body",
  "title" : "test title",
  "content_available" : true,
  "priority" : "high"
 }
}

I hope this solves your problem

Related