【问题标题】:Flutter Notifications with Cloud Functions and Firebase Cloud Messaging don't run in the backgroundFlutter Notifications with Cloud Functions 和 Firebase Cloud Messaging 不在后台运行
【发布时间】:2023-02-11 01:08:38
【问题描述】:

我正在使用 Firebase Firestore、Cloud Functions 和 Messaging 构建一个 Flutter 应用程序。我正在尝试发送和接收通知。

通知在以下情况下有效:

  • 应用程序在前台,我在我的收藏中创建了一个新文档(云函数监听的那个)
  • 应用程序在前台,我使用网站上的 firebase 控制台
  • 当我使用 firebase 控制台时,该应用程序在后台(但仍在运行)

但是,通知在以下情况下不起作用:

  • 应用程序在后台,我在我的收藏中创建了一个新文档
  • 应用终止。我既没有收到来自控制台的通知,也没有收到来自 Cloud Function 的通知。

我已经像这样设置了 Firebase 函数:

import { messaging } from "firebase-admin";
import * as functions from "firebase-functions";
const { initializeApp } = require('firebase-admin/app');

initializeApp();

const token = "<my-token>";

exports.pushNotifications = functions
    .region('europe-central2')
    .firestore.document("signals/{docId}").onCreate(
        (snapshot) => {
            return messaging().send(
                {
                    token: token,
                    data: {
                        title: "A New Notification",
                        body: "Hello world!",
                    }
                }
            );
        }
    );

在前端:

class Notifications {
  static final messagingInstance = FirebaseMessaging.instance;

  static final Stream<RemoteMessage> foregroundNotificationsStream = FirebaseMessaging.onMessage;

  static final StreamSubscription<RemoteMessage> notificationsListener =
      FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    if (message.data.isNotEmpty) {
      String body = '';
      try {
        final payload = message.data['body'];
        if (payload is String) body = payload;
      } catch (e) {
        body = '';
      }
      // Show dialog
    } else if (message.notification != null && message.notification!.body != null) {
      // show dialog
    }
  });

  static void getToken() async {
    await messagingInstance.requestPermission();
    final fcmToken = await messagingInstance.getToken();
    Get.put(User()).updateAppUser(tokenStatus: fcmToken);
    Database.updateDBUser();
  }
}

在清单中:

    <intent-filter>
        <action android:name="FLUTTER_NOTIFICATION_CLICK" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</activity>
<meta-data
    android:name="firebase_messaging_auto_init_enabled"
    android:value="false" />
<meta-data
    android:name="firebase_analytics_collection_enabled"
    android:value="false" />

【问题讨论】:

    标签: flutter firebase dart push-notification firebase-cloud-messaging


    【解决方案1】:

    添加后台消息处理程序。

    @pragma('vm:entry-point')
    Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
      // TODO
    }
    
    void main() {
      FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
      runApp(MyApp());
    }
    

    详情请见Background messages

    当用户单击通知以从终止状态打开应用程序时,使用getInitialMessage 获取消息。

    RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
    if (initialMessage != null) {
      // TODO
    }
    

    有关详细信息,请参阅Handling interaction

    发送组合消息(数据和通知)以在所有应用程序状态或数据消息中提供相同的数据,在这种情况下,应用程序负责显示可见通知。

    const payload = {
      notification: {
        title: "A New Notification",
        body: "Hello world!",day.'
      },
      data: {
        title: "A New Notification",
        body: "Hello world!",day.'
      }
    };
    

    详情请见Message types

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 2020-01-29
      • 2021-04-26
      • 2018-12-25
      • 2018-10-18
      • 2017-08-07
      • 2020-10-01
      • 2016-11-21
      • 2020-04-28
      相关资源
      最近更新 更多