我想在我的flutter应用程序上使用fcm,所以在创建firebase控制台并在我的gradle中安装了一些依赖项后,如下所示:
build.gradle:项目
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.3'
}
build.gradle:应用
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
...
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.firebase:firebase-analytics:17.5.0'
implementation 'com.google.firebase:firebase-messaging:20.2.4'
}
并创建一个Java应用程序类:
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.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) {
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
并在清单中添加这个应用程序类和意图过滤器:
<application
android:name=".Application"
android:label="sian_mobile"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
...
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
并在pubspect 中安装firebase_messaging: ^7.0.0
我写了这个简单的FirebaseMessaging代码:
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (message) async {
print("-------> $message");
},
onResume: (message) async {
print("-------> $message");
},
onLaunch: (message) async {},
);
}
当我运行应用程序并且应用程序已打开时,我会将通知从控制台推送到应用程序。在vscode控制台中,我得到了以下错误:
Installing build/app/outputs/flutter-apk/app.apk...
E/FlutterFcmService(11005): Fatal: failed to find callback
Connecting to VM Service at ws://127.0.0.1:41113/PSCQfaDH4oA=/ws
D/EGL_emulation(11005): eglMakeCurrent: 0xe83ba420: ver 2 0 (tinfo 0xc80dd6e0)
W/.sian_mobil(11005): Accessing hidden method Landroid/os/WorkSource;->add(I)Z (greylist, reflection, allowed)
W/.sian_mobil(11005): Accessing hidden method Landroid/os/WorkSource;->add(ILjava/lang/String;)Z (greylist, reflection, allowed)
W/.sian_mobil(11005): Accessing hidden method Landroid/os/WorkSource;->size()I (greylist, reflection, allowed)
W/.sian_mobil(11005): Accessing hidden method Landroid/os/WorkSource;->get(I)I (greylist, reflection, allowed)
W/.ian_mobil(11005): Accessing hidden method Landroid/os/WorkSource;->getName(I)Ljava/lang/String; (greylist, reflection, allowed)
I/flutter (11005): -------> {notification: {title: test title, body: Test notification text}, data: {}}
当应用程序处于前台时,似乎仍然收到推送消息。该问题可能是由于应用程序处于后台时没有推送消息的处理程序造成的。我建议通过本指南来设置后台消息处理程序。