[英]Flutter: Foreground notification is not working in firebase messaging
I need to show firebase notifications when the app is on foreground by using local notification but it is not working.当应用程序处于前台时,我需要使用本地通知显示 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();
}
Here is the Firebase Code这是 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');
},
);
}
This is showNotification
method这是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,);
}
and Firebase Response和 Firebase 响应
{notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}} {notification: {title: Test Title, body: Test Notification Text}, data: {orderid: 2, click_action: FLUTTER_NOTIFICATION_CLICK, order name: farhana}}
There is an active issue logged on GitHub repository for the package regarding the same.在 GitHub 存储库中记录了一个与该软件包相关的活动问题。 Firebase messaging and local notifications won't work together on iOS since you can register only a single delegate for notifications. Firebase 消息传递和本地通知无法在 iOS 上协同工作,因为您只能注册一个通知代理。
Check out: https://github.com/MaikuB/flutter_local_notifications/issues/111查看: https : //github.com/MaikuB/flutter_local_notifications/issues/111
There's also an active flutter issue for the same: https://github.com/flutter/flutter/issues/22099同样还有一个活跃的颤振问题: https : //github.com/flutter/flutter/issues/22099
Problem: See the Push Notification while application is in foreground.问题:在应用程序处于前台时查看推送通知。
Solution: I was using firebase_message plugin and I was able to see the Push Notification while application is in foreground by making these few changes in my flutter project's iOS AppDelegate.swift file in flutter project.解决方案:我正在使用 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])
}
}
Note: This also works while using with flutter_local_notification plugin but with an issue that onSelectNotification is not working due to changes done above.注意:这在与 flutter_local_notification 插件一起使用时也有效,但存在一个问题,即由于上述更改,onSelectNotification 无法正常工作。
You can find the answer in FlutterFire documentation https://firebase.flutter.dev/docs/migration/#messaging您可以在 FlutterFire 文档https://firebase.flutter.dev/docs/migration/#messaging 中找到答案
You just add to your code the following line您只需将以下行添加到您的代码中
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
如果没有在前台收到通知,请确保 android 可绘制文件包含 launcher_icon 或您在 showotification 函数中设置的图标。
I also couldn't get firebase notifications with flutter_local_notifications working on iOS in foreground.我也无法在前台的 iOS 上使用 flutter_local_notifications 获得 firebase 通知。 Background notifications worked fine.后台通知工作正常。 Problem was that firebase notification data is different on iOS.问题是 iOS 上的 Firebase 通知数据不同。 Notification data is not ["notification"]["title"]
as in android but ["aps"]["alert"]["title"]
.通知数据不是["notification"]["title"]
就像在 android 中一样,而是["aps"]["alert"]["title"]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.