Firebase Cloud Message - tungpham6195/flutter GitHub Wiki

package on pub.dev

Android Integration

  1. download google-services.json to android/app from Firebase Console

  2. Add the classpath to the [project]/android/build.gradle

dependencies {
  // Add the google services classpath
  classpath 'com.google.gms:google-services:4.3.2'
}
  1. Add the apply plugin to the [project]/android/app/build.gradle
apply plugin: ...
apply plugin: 'com.google.gms.google-services'
  1. If want to be notified in your app (via onResume and onLaunch), add intent-filter within the <activity> tag of your android/app/src/main/AndroidManifest.xml
 <intent-filter>
      <action android:name="FLUTTER_NOTIFICATION_CLICK" />
      <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

By default background messaging is not enabled. To handle messages in the background:

  1. add com.google.firebase:firebase-messaging to <app-name>/android/app/build.gradle
dependencies {
  implementation 'com.google.firebase:firebase-messaging:<latest_version>'
}

find latest version of Cloud Messaging

  1. add Application.java, (must be java class, not kotlin)
...

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
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);
    }
}

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

NOTE: issue about why need to create class FirebaseCloudMessagingPluginRegistrant link, will be updated if flutter team know this

  1. Set name property of application in AndroidManifest.xml
<application android:name=".Application" ...>

iOS Integration

  1. Generate the certificates required by Apple for receiving push notifications

  1. download GoogleService-Info.plist to ios/Runner
  2. In Xcode, select Runner in the Project Navigator. In the Capabilities Tab turn on Push Notifications and Background Modes, and enable Background fetch and Remote notifications under Background Modes.

  1. Upload your APNs certificate to Firebase Console -> Project Settings -> Cloud Messaging -> iOS app configuration
  • create APN key

  • upload APN key

  1. add this code to Runner/AppDelegate.swift inside of function func application
 GeneratedPluginRegistrant.register(with: self)
 ...

 //this codes
 if #available(iOS 10.0, *) { 
      UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
 }

 ...

Flutter Setup

⚠️ **GitHub.com Fallback** ⚠️