【问题标题】:Flutter: Foreground notification is not working in firebase messagingFlutter:前台通知在 Firebase 消息传递中不起作用
【发布时间】:2019-07-05 16:46:47
【问题描述】:

当应用程序在前台时,我需要使用本地通知显示 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 响应

{通知:{title:测试标题,正文:测试通知文本},数据:{orderid:2,click_action:FLUTTER_NOTIFICATION_CLICK,订单名称:farhana}}

【问题讨论】:

  • 你解决了吗?
  • @Farhana 你能解决这个问题吗?我也在苦苦挣扎。
  • @Rocx 是的,我解决了这个问题
  • 你能分享一些工作代码吗?还想了解我是否在不同的页面上,然后我会收到 onmessage 通知吗?或者需要在任何地方定义它。 stackoverflow.com/questions/65166526/…

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


【解决方案1】:

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

您只需在代码中添加以下行

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

【讨论】:

  • 哇!这是正确的答案。谢谢。注意:请务必在您的应用初始化之后添加此,即在await Firebase.initializeApp(); 之后
  • 随时兄弟@lenz
【解决方案2】:

在 GitHub 存储库中记录了一个关于该包的活动问题。 Firebase 消息传递和本地通知无法在 iOS 上同时使用,因为您只能注册一个代表来接收通知。

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

同样还有一个活跃的颤振问题: https://github.com/flutter/flutter/issues/22099

【讨论】:

  • 我正在为 Android 工作,稍后我将在 ios 工作,感谢您提供的信息
  • 您是否在 Android 上遇到任何错误或异常?或者通知不会显示?
  • 我检查了调试模式,但没有异常
  • 我刚刚在 Android 上测试了上面的代码,它对我有用。你能检查一下你的代码到底在哪里失败了吗? showNotification() 中的 print(msg) 语句是否被执行?
  • 其实我是在oreo devic中测试的;
【解决方案3】:

问题: 在应用程序处于前台时查看推送通知。

解决方案: 我正在使用 firebase_message 插件,并且通过在 Flutter 项目中的 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 无法正常工作。

【讨论】:

  • 我可以在前台看到通知,但我的 onMessage 没有被调用。有什么解决办法吗?
  • 如果您在 didFinishLaunchingWithOptions 中设置了委托,那么如果您在 FCM 有效负载中发送通知对象,并且如果您删除用户通知委托方法,那么您将无法显示通知,则显示通知将不起作用
【解决方案4】:

如果在前台没有收到通知,请确保 android drawable 文件包含 launcher_icon 或您在 shownotification 函数中设置的图标。

【讨论】:

    【解决方案5】:

    我也无法通过 flutter_local_notifications 在前台的 iOS 上获得 firebase 通知。后台通知工作正常。问题是 iOS 上的 firebase 通知数据不同。通知数据不是 android 中的 ["notification"]["title"] 而是 ["aps"]["alert"]["title"]

    【讨论】:

      猜你喜欢
      • 2020-09-01
      • 2021-02-19
      • 2021-07-22
      • 2020-08-14
      • 2020-06-06
      • 2021-08-21
      • 2019-10-08
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多