簡體   English   中英

Flutter:前台通知在 Firebase 消息傳遞中不起作用

[英]Flutter: Foreground notification is not working in firebase messaging

當應用程序處於前台時,我需要使用本地通知顯示 firebase 通知,但它不起作用。

  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin=new FlutterLocalNotificationsPlugin();

  static  FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  static StreamController<Map<String, dynamic>> _onMessageStreamController =
  StreamController.broadcast();
  static StreamController<Map<String, dynamic>> _streamController =
  StreamController.broadcast();
  static final Stream<Map<String, dynamic>> onFcmMessage =
      _streamController.stream;

  @override
  void initState() {
    super.initState();

    var android=AndroidInitializationSettings('mipmap/ic_launcher.png');
    var ios=IOSInitializationSettings();
    var platform=new  InitializationSettings(android,ios);
    flutterLocalNotificationsPlugin.initialize(platform);

    firebaseCloudMessaging_Listeners();
  }

這是 Firebase 代碼

 void firebaseCloudMessaging_Listeners() {

if (Platform.isIOS) iOS_Permission();

_firebaseMessaging.getToken().then((token) {
  print("FCM TOKEN--" + token);
});
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print('on message $message');
    showNotification(message);
  },
  onResume: (Map<String, dynamic> message) async {
    print('on resume $message');
  },
  onLaunch: (Map<String, dynamic> message) async {
    print('on launch $message');
  },
);


}

這是showNotification方法

 void showNotification(Map<String, dynamic> msg) async{
    print(msg);
    var android = new AndroidNotificationDetails(
        'my_package', 'my_organization', 'notification_channel', importance: Importance.Max, priority: Priority.High);
    var iOS = new IOSNotificationDetails();
    var platform=new NotificationDetails(android, iOS);
    await flutterLocalNotificationsPlugin.show(
      0,'My title', 'This is my custom Notification', platform,);
  }

和 Firebase 響應

{notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}}

在 GitHub 存儲庫中記錄了一個與該軟件包相關的活動問題。 Firebase 消息傳遞和本地通知無法在 iOS 上協同工作,因為您只能注冊一個通知代理。

查看: https : //github.com/MaikuB/flutter_local_notifications/issues/111

同樣還有一個活躍的顫振問題: https : //github.com/flutter/flutter/issues/22099

問題:在應用程序處於前台時查看推送通知。

解決方案:我正在使用 firebase_message 插件,通過在我的 Flutter 項目的 iOS AppDelegate.swift 文件中進行這些更改,我能夠在應用程序處於前台時看到推送通知。

import UIKit
import Flutter
import UserNotifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate, UNUserNotificationCenterDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
  ) -> Bool {
    GeneratedPluginRegistrant.register(with: self)
    // set the delegate in didFinishLaunchingWithOptions
    UNUserNotificationCenter.current().delegate = self
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
    // This method will be called when app received push notifications in foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        completionHandler([.alert, .badge, .sound])
    }
}

注意:這在與 flutter_local_notification 插件一起使用時也有效,但存在一個問題,即由於上述更改,onSelectNotification 無法正常工作。

您可以在 FlutterFire 文檔https://firebase.flutter.dev/docs/migration/#messaging 中找到答案

您只需將以下行添加到您的代碼中

    FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);

如果沒有在前台收到通知,請確保 android 可繪制文件包含 launcher_icon 或您在 showotification 函數中設置的圖標。

我也無法在前台的 iOS 上使用 flutter_local_notifications 獲得 firebase 通知。 后台通知工作正常。 問題是 iOS 上的 Firebase 通知數據不同。 通知數據不是["notification"]["title"]就像在 android 中一樣,而是["aps"]["alert"]["title"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM