我遇到了 Firebase Cloud Messaging(FCM) 的问题。我的问题是:在最近的选项卡中关闭应用程序时,设备未收到 FCM 推送通知。 当应用程序在前台或后台时,它工作正常。但是,当我从后台关闭它时,我没有在通知托盘中收到任何通知。
我看了很多帖子并尝试了很多解决方案,但在我的情况下没有任何效果:(
我尝试过的步骤:
app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 29
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.myapplication.id"
minSdkVersion 16
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:26.5.0')
implementation 'com.google.firebase:firebase-analytics'
implementation "androidx.browser:browser:1.3.0"
implementation 'com.google.firebase:firebase-messaging:21.0.1'
}
2.我在我的 project/build.gradle 中添加了 google-services 依赖
这是我的project/build.gradle
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
app/src/main/Manifest.xml
<application
android:label="partner_location_project"
android:icon="@mipmap/ic_launcher">
<activity
android:exported="true"
android:enabled="true"
android:name=".MainActivity"
....
....
....
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
这是我声明 FCM 的 dart 文件。
class _CheckAuthState extends State<CheckAuth> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
void fcm_listener() async {
try {
var token = await _firebaseMessaging.getToken();
print("fcm token = $token");
_firebaseMessaging.configure(
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch $message");
},
onMessage: (Map<String, dynamic> message) async {
print("onMessage $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume $message");
},
);
} catch (e) {
print("fcm exception");
print(e.toString());
}
}
void initState() {
// TODO: implement initState
super.initState();
fcm_listener();
}
我正在真实设备(realme 6 pro)上进行测试。 (我正在使用 firebase_messaging:^9.0.0) 任何想法表示赞赏!
答案 0 :(得分:1)
检查这个工作示例:Flutter_Firebase_Notification
示例代码:
Future<void> myBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
return MyAppState()._showNotification(message);
}
Future<void> main() async {
await WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(myBackgroundHandler);
runApp(MaterialApp(home: MyApp()));
}