Push notification not working when the app is closed - Flutter

When the application is closed my Android doesn't receive push notifications from Firebase, but if I open the app, everything works. Here is my MainActivity:

package com.imperio.app

import io.flutter.embedding.android.FlutterActivity

class MainActivity: FlutterActivity() {
}

And my Flutter configure:

void configure() {
    _firebaseMessaging.requestNotificationPermissions();

    _firebaseMessaging.configure(
      onLaunch: (params) {
        _bloc.onEventChanged(DispatchLaunch(params));
        return null;
      }, 
      onMessage: (params) {
        _bloc.onEventChanged(DispatchMessage(params));
        return null;
      }, 
      onResume: (params) {
        _bloc.onEventChanged(DispatchLaunch(params));
        return null;
      }
    );  
  }

When the application is open, I am receiving notifications normally. I see some people changing de MainActivity, but after embedded v2, Google documentation says nothing about it.

I added notification clicked in Android Manifest:

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

The problem is happening in Android 10, I tested in a Android 8 device and it works.

Upvotes: 1

Views: 2235

Answers (1)

Biruk Telelew
Biruk Telelew

Reputation: 1193

in flutters firebase messaging documetation you have to add

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

inside tag of mainfest.xml

and add Application.java and paste this code

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);
   }
  }

Set name property of application in AndroidManifest.xml. This is typically found in /android/app/src/main/.

<application android:name=".Application" ...>

you can get full documentation in https://pub.dev/packages/firebase_messaging

Upvotes: 1

Related Questions