【发布时间】: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