其实文档说的还算比较详细,但有些东西没有更新,照着文档无法正常集成。
pub地址
添加依赖
# firebase
firebase_messaging: ^6.0.16
在firebase后台添加应用
使用Firebase控制台将Android应用添加到您的项目中:跟随助手,下载生成的google-services.json文件,并将其放置在android / app中。
dependencies {
// Example existing classpath
classpath 'com.android.tools.build:gradle:3.5.3'
// Add the google services classpath
classpath 'com.google.gms:google-services:4.3.2'
}
2.1 将apply插件添加到[project] /android/app/build.gradle文件中。
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
2.2 设置通知栏的点击回调
如果用户希望在用户点击系统任务栏中的通知时在您的应用中收到通知(通过onResume和onLaunch,请参见下文),请在android /app / src / main / AndroidManifest.xml:
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
intent-filter>
2.3 添加后台推送
在通常位于 /android/app/build.gradle的应用程序级build.gradle文件中添加com.google.firebase:firebase-messaging依赖项。
dependencies {
// ...
implementation 'com.google.firebase:firebase-messaging:'
}
您可以在此处(Cloud Messaging)中找到该插件的最新版本。
2.4 在与MainActivity.java相同的目录中,将Application.java类添加到您的应用程序中。通常可以在 / android / app / src / main / java / /中找到。
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
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"));
}
}
2.5 在AndroidManifest.xml中设置应用程序的名称属性。通常可以在 / android / app / src / main /中找到。
<application android:name=".Application" ...>
2.6 定义一个TOP-LEVEL或STATIC函数来处理后台消息
Future myBackgroundMessageHandler(Map message) {
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
// Or do other work.
}
注意:data
和 notification
与RemoteMessage定义的字段一致。
2.7 在调用configure
时设置onBackgroundMessage
处理程序
_firebaseMessaging.configure(
onMessage: (Map message) async {
print("onMessage: $message");
_showItemDialog(message);
},
//处理后台推送
onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
onLaunch: (Map message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
onResume: (Map message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
注意:应该在应用程序生命周期的早期调用“ configure”以便可以尽早接收消息。见[示例应用](https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging/example)进行演示。
3.1 遵循Firebase文档中的本指南,生成Apple接收推送通知所需的证书。您可以跳过标题为“创建配置文件”的部分。
3.2 使用[Firebase控制台](https://console.firebase.google.com/)将iOS应用添加到您的项目中:跟随文档,下载生成的GoogleService-Info.plist
文件,打开ios /带有Xcode的Runner.xcworkspace
,并在Xcode中将文件放置在ios / Runner
中。不要在Firebase助手中执行名为“添加Firebase SDK”和“添加初始化代码”的步骤。
3.3 在Xcode中,在Project Navigator中选择Runner
。在“功能”标签中,打开Push Notifications
和Background Modes
,并在“背景模式”下启用Remote notifications
和Background Modes
。
3,4 按照Firebase文档的“ [上传您的APNs证书](https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_certificate)”部分中的步骤操作。
3.5 如果您需要禁用FCM iOS SDK完成的方法转换(例如,以便可以将此插件与其他通知插件一起使用),则将以下内容添加到应用程序的“ Info.plist”文件中。
<key>FirebaseAppDelegateProxyEnabledkey>
<false/>
3.6 之后,将以下行添加到(BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions:(NSDictionary *)launchOptions`iOS项目的AppDelegate.m / AppDelegate.swift中的方法。
Objective-C:
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
Swift:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
From your Dart code, you need to import the plugin and instantiate it:
import 'package:firebase_messaging/firebase_messaging.dart';
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
接下来,您可能应该请求接收推送通知的权限。为此,请调用_firebaseMessaging.requestNotificationPermissions()
。
这将打开一个权限对话框,供用户在iOS上进行确认。Android上无人操作。最后一点,是通过_firebaseMessaging.configure()来注册onMessage,onResume和onLaunch回调以侦听传入的消息(有关更多信息,请参见下表)
消息将通过在安装过程中使用插件配置的onMessage,onLaunch和onResume回调发送到Flutter应用。以下是在支持的平台上传递不同消息类型的方式:
App在前台 | App在后台 | App已终止 | |
---|---|---|---|
Notification on Android | onMessage |
通知已传递到系统托盘。当用户单击它以打开应用程序时,如果设置了“ click_action:FLUTTER_NOTIFICATION_CLICK”(见下文),则会触发onResume。 | 通知已传递到系统托盘。当用户单击它以打开应用程序时,如果设置了“ click_action:FLUTTER_NOTIFICATION_CLICK”(见下文),onLaunch就会触发。 |
Notification on iOS | onMessage |
通知已传递到系统托盘。当用户单击它以打开应用程序时,onResume将触发。 | 通知已传递到系统托盘。当用户点击它打开应用时,onLaunch 会触发。 |
Data Message on Android | onMessage |
当应用程序停留在后台时,onMessage 。 |
插件不支持,消息丢失 |
Data Message on iOS | onMessage |
消息由FCM存储,并在应用返回到前台时通过onMessage传递给应用。 | 消息由FCM存储,并在应用返回到前台时通过onMessage传递给应用。 |
其他阅读:Firebase的[关于FCM消息](https://firebase.google.com/docs/cloud-messaging/concept-options)。
通过将其他数据添加到通知消息的“ data”字段中,可以在通知消息中包含其他数据。在Android上,消息包含一个包含数据的附加字段data
。在iOS上,数据直接附加到消息中,并且省略了额外的“ data”字段。要在两个平台上接收数据:
Future _handleNotification (Map message, bool dialog) async {
var data = message['data'] ?? message;
String expectedAttribute = data['expectedAttribute'];
/// [...]
}
有关将消息发送到您的应用程序的所有详细信息,请参阅[Firebase文档](https://firebase.google.com/docs/cloud-messaging/)。
在向Android设备发送通知消息时,您需要确保将消息的click_action
属性设置为FLUTTER_NOTIFICATION_CLICK
。
否则,当用户在系统任务栏中单击该插件时,该插件将无法将通知传递给您的应用程序。
出于测试目的,发送通知的最简单方法是通过[Firebase控制台](https://firebase.google.com/docs/cloud-messaging/send-with-console)。
定位到Android设备时,请确保将“ click_action:FLUTTER_NOTIFICATION_CLICK”作为“自定义数据”键值对(在“高级选项”下)包括在内。
Firebase控制台不支持发送数据消息。
Android:
I/flutter (27265): onMessage: {notification: {title: 标题, body: 内容}, data: {data: asda, click_action: FLUTTER_NOTIFICATION_CLICK}}
APP在后台运行时,收到推送会由系统处理并发送到通知栏,只有在点击时才会触发onResume
点击通知:
I/flutter (27265): onResume: {notification: {}, data: {collapse_key: com.cece.app, data: asda, google.original_priority: high, google.sent_time: 1597290543985, google.delivered_priority: high, google.ttl: 2419200, from: 818960474503, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:1597290544292403%e9ecbb44e9ecbb44}}
iOS
前台:
onMessage:
{
from: 818960474503,
collapse_key: com.cece.app,
notification:
{ body: 1111,
title: 阿斯达,
e: 1,
tag: campaign_collapse_key_5272206810234597867
}
}