Firebase Cloud Message - tungpham6195/flutter GitHub Wiki
-
download
google-services.json
to android/app from Firebase Console -
Add the classpath to the
[project]/android/build.gradle
dependencies {
// Add the google services classpath
classpath 'com.google.gms:google-services:4.3.2'
}
- Add the apply plugin to the
[project]/android/app/build.gradle
apply plugin: ...
apply plugin: 'com.google.gms.google-services'
- If want to be notified in your app (via onResume and onLaunch), add
intent-filter
within the<activity>
tag of yourandroid/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:
- 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
- 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
- Set name property of application in
AndroidManifest.xml
<application android:name=".Application" ...>
- Generate the certificates required by Apple for receiving push notifications
- download
GoogleService-Info.plist
toios/Runner
- In Xcode, select
Runner
in the Project Navigator. In the Capabilities Tab turn onPush Notifications
andBackground Modes
, and enableBackground fetch
andRemote notifications
under Background Modes.
- Upload your APNs certificate to Firebase Console ->
Project Settings
->Cloud Messaging
->iOS app configuration
- create APN key
- upload APN key
- add this code to
Runner/AppDelegate.swift
inside of functionfunc application
GeneratedPluginRegistrant.register(with: self)
...
//this codes
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
...