Flutter推送通知插件push_notification_firebase的使用
Flutter推送通知插件push_notification_firebase的使用
本项目是一个新的Flutter应用程序。
应用配置以支持Firebase推送通知
创建Firebase项目
为您的独立项目创建一个Firebase项目。
设置和更改包名(可选)
包名可以在您的Flutter项目的./android/app/build.gradle
文件中找到。
下载并添加google-services.json
注册后,将生成一个google-services.json
文件,该文件将我们的Flutter应用与Firebase Google服务关联起来。下载该文件,并将其移动到Flutter项目的./android/app
目录下。
添加Firebase配置到原生文件
在根级别(项目级别)的Gradle文件(android/build.gradle
)中,我们需要添加规则以包含Google Services Gradle插件。
buildscript {
repositories {
// 确保你有以下这一行(如果没有,则添加它):
google() // Google的Maven仓库
}
dependencies {
...
// 添加这一行
classpath 'com.google.gms:google-services:4.3.4'
}
}
allprojects {
...
repositories {
// 确保你有以下这一行(如果没有,则添加它):
google() // Google的Maven仓库
...
}
}
应用Google Services Gradle插件
在应用级别(android/app/build.gradle
)的Gradle文件中,我们需要应用Google Services Gradle插件。
// 添加以下这行:
apply plugin: 'com.google.gms.google-services' // Google Services插件
将Firebase Messaging集成到Flutter
在./android/app/build.gradle
文件中添加firebase-messaging
依赖项。
dependencies {
// 添加这一行
implementation platform('com.google.firebase:firebase-bom:32.1.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
root level build.gradle
dependencies {
// 添加这一行
classpath 'com.google.gms:google-services:4.3.15'
}
设置minSdkVersion
minSdkVersion
应设置为19。
在Android清单文件中添加权限
这些权限需要在Android清单文件中:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
构建默认通知渠道
此代码应在Android清单文件中:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="high_importance_channel" />
更多关于Flutter推送通知插件push_notification_firebase的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter推送通知插件push_notification_firebase的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
push_notification_firebase
是一个用于在 Flutter 应用中集成 Firebase Cloud Messaging (FCM) 推送通知的插件。通过这个插件,你可以轻松地在应用中接收和处理来自 Firebase 的推送通知。以下是使用 push_notification_firebase
插件的基本步骤:
1. 添加依赖
首先,你需要在 pubspec.yaml
文件中添加 push_notification_firebase
插件的依赖。
dependencies:
flutter:
sdk: flutter
push_notification_firebase: ^latest_version
然后运行 flutter pub get
来获取依赖。
2. 配置 Firebase
在使用 push_notification_firebase
之前,你需要在 Firebase 控制台中创建一个项目,并为你的 Flutter 应用配置 Firebase。
- 创建 Firebase 项目:访问 Firebase 控制台 并创建一个新项目。
- 添加 Android 应用:在 Firebase 控制台中,点击“添加应用”并选择 Android。按照提示输入应用的包名,并下载
google-services.json
文件。 - 添加 iOS 应用:类似地,添加 iOS 应用并下载
GoogleService-Info.plist
文件。 - 将配置文件添加到项目中:将
google-services.json
文件放在android/app
目录下,将GoogleService-Info.plist
文件放在ios/Runner
目录下。
3. 配置 Android
在 android/app/build.gradle
文件中,确保启用了 Firebase 服务。
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services' // 添加这一行
android {
// 其他配置
}
dependencies {
implementation platform('com.google.firebase:firebase-bom:29.0.0') // 添加这一行
implementation 'com.google.firebase:firebase-messaging' // 添加这一行
}
// 添加这一行
apply plugin: 'com.google.gms.google-services'
4. 配置 iOS
在 ios/Runner/Info.plist
文件中,添加以下配置:
<key>FirebaseAppDelegateProxyEnabled</key>
<true/>
5. 初始化 Firebase
在你的 Flutter 应用中,初始化 Firebase。
import 'package:push_notification_firebase/push_notification_firebase.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await PushNotificationFirebase.initialize();
runApp(MyApp());
}
6. 处理通知
你可以通过 PushNotificationFirebase
来监听和处理通知。
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
_initializePushNotifications();
}
void _initializePushNotifications() {
PushNotificationFirebase.onMessage.listen((RemoteMessage message) {
print("收到通知: ${message.notification?.title}");
// 处理通知
});
PushNotificationFirebase.onMessageOpenedApp.listen((RemoteMessage message) {
print("通过点击通知打开应用: ${message.notification?.title}");
// 处理通知
});
PushNotificationFirebase.onBackgroundMessage((RemoteMessage message) async {
print("后台消息: ${message.notification?.title}");
// 处理通知
return Future.value(true);
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
7. 获取设备令牌
你可以通过 PushNotificationFirebase.getToken()
获取设备的 FCM 令牌。
void _getToken() async {
String? token = await PushNotificationFirebase.getToken();
print("FCM Token: $token");
}
8. 发送测试通知
你可以通过 Firebase 控制台或使用 Postman 发送测试通知到你的设备。
9. 处理后台通知
为了确保后台通知能够正常工作,你需要在 AndroidManifest.xml
文件中配置后台服务。
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>