Flutte接入firebase messaging(FCM)

Flutte接入firebase messaging(FCM)flutter 接入 firebasemess 其实文档说的还算比较详细 但有些东西没有更新 照着文档无法正常集成 pub 地址使用添加依赖 firebasefire messaging 6 0 16Android 配置在 firebase 后台添加应用使用 Firebase 控制台将 Android 应用添加到您的项目中 跟随助手 下载生成的 google services json 文件 并将其放置在 android app 中 dependencies

flutter接入firebase messaging

使用

  1. 添加依赖
     # firebase firebase_messaging: ^6.0.16 
  2. Android配置

在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. } 
    
  

注意:datanotification与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)进行演示。

  1. iOS配置

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 NotificationsBackground Modes,并在“背景模式”下启用Remote notificationsBackground 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>FirebaseAppDelegateProxyEnabled 
     key> <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 } 

Dart/Flutter Integration

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控制台不支持发送数据消息。

APP收到的消息格式

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: 85, google.delivered_priority: high, google.ttl: , from: 3, click_action: FLUTTER_NOTIFICATION_CLICK, google.message_id: 0:92403%e9ecbb44e9ecbb44}}

iOS
前台:

 onMessage: { from: 3, collapse_key: com.cece.app, notification: { body: 1111, title: 阿斯达, e: 1, tag: campaign_collapse_key_ } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/221293.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午6:28
下一篇 2026年3月17日 下午6:28


相关推荐

  • aardio – MsSql数据库+虚表示例

    aardio – MsSql数据库+虚表示例给同事写了个数据录入的小程序 算是一个简单的 mssql 数据库 虚表 示例 主窗口主窗口设计界面 主窗口运行界面 主窗口代码 importfonts fontAwesome importwin ui importwin ui ctrl vlistEx DSG mainForm win form text 张某的程序 right 959 bottom 591 image res 315b100ca64e

    2026年3月16日
    1
  • 在Ubuntu/Linux环境下使用MySQL:开放/修改3306端口、开放访问权限「建议收藏」

    在Ubuntu/Linux环境下使用MySQL:开放/修改3306端口、开放访问权限「建议收藏」一、查看3306端口是否开放netstat-an|grep3306如果看到下图这样的,说明端口并未打开:二、修改访问权限进入目录“etc/mysql/mysql.conf.d/”,如下图所示:在这个目录下,有一个配置文件“mysqld.cnf”,如下图所示:打开这个配置文件:sudovimmysqld.cnf文件打开后有一大段注释说明,不…

    2022年8月30日
    4
  • VMware 虚拟机如何连接网络「建议收藏」

    VMware 虚拟机如何连接网络「建议收藏」ps:本教程是针对虚拟机NAT模式连接网络一、首先查看自己的虚拟机服务有没有开启,选择电脑里面的服务查看;1.计算机点击右键选择管理2.进入管理选择VM开头的服务如果没有开启的话就右键开启二、虚拟机服务开启后就查看本地网络虚拟机的网卡启动没有1.电脑右下角网络标志右键进入网络和共享中心2.点击更改适配器,查看虚拟机的虚拟网卡启动没有,没有启动的话右键点击启动3.网卡开启后设置ip地址

    2022年4月20日
    2.8K
  • 多层感知机和神经网络_CNN采用多层感知机进行分类

    多层感知机和神经网络_CNN采用多层感知机进行分类单独的ESN仿真:ESN的运行结果如下所示:这个部分的误差为:0.0435ESN部分就不多做介绍了,你应该了解的,下面我们对ESN和BP改进和极限学习改进分别进行修改和说明,并进行仿真。ESN+BP的仿真:首先,在原始的ESN中,权值的计算是通过pseudoinverse.m这个函数进行计算的,其主要内容就是:即:这里,我们的主要方法…

    2022年8月30日
    7
  • 开启1521端口监听_服务器1521端口被关闭,如何开启?

    开启1521端口监听_服务器1521端口被关闭,如何开启?展开全部方法如下:1、修改远程桌面连接端口:(1)远程桌面终端服务默认端口为“3389”,为防止他人进行恶意连接,就需要32313133353236313431303231363533e78988e69d8331333365633964对默认端口进行更改。(2)对此可打开注册表编辑器,依次展开“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\…

    2022年6月10日
    155
  • 迎接平价时代 光伏逆变器行业的演进和格局「建议收藏」

    迎接平价时代 光伏逆变器行业的演进和格局

    2022年3月4日
    48

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号