Flutter FCM通知管理插件e_notification_platform_fcm的使用
Flutter FCM通知管理插件e_notification_platform_fcm的使用
如何使用
Android
在 android/app
目录下放置 google-services.json
文件
google-services.json 文件,需要在 Firebase 控制台生成
在 android/build.gradle
加入以下文本
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+classpath 'com.google.gms:google-services:4.3.8' // 添加 Google Services 插件
}
}
在 android/app/build.gradle
加入以下文本
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.android.application'
+apply plugin: 'com.google.gms.google-services' // 启用 Google Services 插件
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
iOS
在 ios
目录下放置 GoogleService-Info.plist
文件
GoogleService-Info.plist 文件,需要在 Firebase 控制台生成
完整示例Demo
以下是一个完整的 Flutter 应用示例,展示如何集成和使用 e_notification_platform_fcm 插件来处理 FCM 通知。
项目结构
your_flutter_project/
├── android/
│ ├── app/
│ │ ├── src/
│ │ │ └── main/
│ │ │ └── res/
│ │ └── build.gradle
│ └── build.gradle
├── ios/
│ ├── Runner.xcworkspace
│ └── GoogleService-Info.plist
├── lib/
│ └── main.dart
└── pubspec.yaml
步骤 1: 配置 Firebase
- 登录 Firebase 控制台 并创建一个新项目。
- 在 Firebase 控制台中,选择您的项目并添加 Android 和 iOS 平台。
- 下载
google-services.json
和GoogleService-Info.plist
文件,并分别放置到android/app/
和ios/
目录中。
步骤 2: 修改 android/build.gradle
在 android/build.gradle
文件中添加以下依赖:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.8' // 添加 Google Services 插件
}
}
步骤 3: 修改 android/app/build.gradle
在 android/app/build.gradle
文件中启用 Google Services 插件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services' // 启用 Google Services 插件
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
步骤 4: 编写 Flutter 代码
在 lib/main.dart
中编写以下代码:
import 'package:flutter/material.dart';
import 'package:e_notification_platform_fcm/e_notification_platform_fcm.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String token = '';
@override
void initState() {
super.initState();
initializeFCM();
}
Future<void> initializeFCM() async {
try {
// 获取 FCM Token
token = await ENotificationPlatformFcm.getFCMToken();
print('FCM Token: $token');
} catch (e) {
print('Error getting FCM Token: $e');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('FCM Notification Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('FCM Token: $token'),
ElevatedButton(
onPressed: () {
// 发送测试通知
sendTestNotification();
},
child: Text('Send Test Notification'),
),
],
),
),
);
}
Future<void> sendTestNotification() async {
try {
// 模拟发送通知
print('Sending test notification...');
await ENotificationPlatformFcm.sendNotification(
to: token, // 使用当前设备的 FCM Token
title: 'Test Notification',
body: 'This is a test notification.',
);
print('Test notification sent successfully.');
} catch (e) {
print('Error sending test notification: $e');
}
}
}
运行应用
- 确保已安装 Flutter SDK 和 Android/iOS 开发环境。
- 在终端中运行以下命令以启动应用:
flutter run
更多关于Flutter FCM通知管理插件e_notification_platform_fcm的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter FCM通知管理插件e_notification_platform_fcm的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
e_notification_platform_fcm
是一个用于在 Flutter 应用中管理 FCM (Firebase Cloud Messaging) 通知的插件。它可以帮助你更轻松地处理 FCM 通知,包括接收、显示和管理通知。
安装
首先,你需要在 pubspec.yaml
文件中添加 e_notification_platform_fcm
插件的依赖:
dependencies:
flutter:
sdk: flutter
e_notification_platform_fcm: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
配置 Firebase
-
创建 Firebase 项目:在 Firebase 控制台 中创建一个新项目。
-
添加 Android 应用:
- 在 Firebase 控制台中,点击 “Add app”,选择 Android。
- 输入你的应用包名(例如
com.example.app
)。 - 下载
google-services.json
文件并将其放置在你的 Flutter 项目的android/app
目录下。
-
添加 iOS 应用:
- 在 Firebase 控制台中,点击 “Add app”,选择 iOS。
- 输入你的 Bundle ID。
- 下载
GoogleService-Info.plist
文件并将其放置在你的 Flutter 项目的ios/Runner
目录下。
-
配置 Android 和 iOS 项目:
-
对于 Android,确保在
android/build.gradle
文件中添加了以下内容:dependencies { classpath 'com.google.gms:google-services:4.3.10' // 使用最新版本 }
并在
android/app/build.gradle
文件末尾添加:apply plugin: 'com.google.gms.google-services'
-
对于 iOS,确保在
ios/Podfile
文件中添加了以下内容:target 'Runner' do use_frameworks! # Pods for Runner pod 'Firebase/Core' pod 'Firebase/Messaging' end
-
使用 e_notification_platform_fcm
初始化
在你的 Flutter 应用中初始化 FCM 和 e_notification_platform_fcm
:
import 'package:e_notification_platform_fcm/e_notification_platform_fcm.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 FCM
await Firebase.initializeApp();
// 初始化 e_notification_platform_fcm
await ENotificationPlatformFcm.initialize();
runApp(MyApp());
}
监听通知
你可以通过 ENotificationPlatformFcm
来监听和显示通知:
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
// 监听通知
ENotificationPlatformFcm.onMessage.listen((RemoteMessage message) {
print("Received notification: ${message.notification?.title}");
// 在这里处理通知,例如显示一个 SnackBar 或 Dialog
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Received notification: ${message.notification?.title}")),
);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('FCM Notification Example'),
),
body: Center(
child: Text('Hello, FCM!'),
),
),
);
}
}
获取 FCM Token
你可以通过 ENotificationPlatformFcm
获取 FCM Token:
String? fcmToken = await ENotificationPlatformFcm.getToken();
print("FCM Token: $fcmToken");
处理后台和终止状态的通知
对于后台和终止状态的通知,你需要处理 onBackgroundMessage
回调。在 main.dart
中添加以下代码:
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
// 在这里处理后台通知
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 FCM
await Firebase.initializeApp();
// 设置后台消息处理程序
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
// 初始化 e_notification_platform_fcm
await ENotificationPlatformFcm.initialize();
runApp(MyApp());
}