[英]Firebase Cloud Messaging push notification not working in foreground, only background, on Android
[英]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.