gpt4 book ai didi

firebase_messaging 如何清除通知?

转载 作者:行者123 更新时间:2023-12-03 04:01:28 24 4
gpt4 key购买 nike

我正在使用 firebase_messaging
当通知到来时,我正在显示警报对话框。下面是我的代码。

showNotification(BuildContext context) {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
_showPushNotificationDialog(message['notification']['title'],
message['notification']['body'], context);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
_showPushNotificationDialog(
message['data']['title'], message['data']['body'], context);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
_showPushNotificationDialog(
message['data']['title'], message['data']['body'], context);
},
);
}

哪里 _showPushNotificationDialog方法每次都会在 时调用留言 , 简历 , 和 上线 方法调用。

面临诸如当我的应用程序处于后台或终止模式时会出现通知并将点击通知托盘的问题,一切都很好。但是当我转到其他页面并一直回到上一个页面时 _firebaseMessaging.configure(....方法调用及其有数据,所以每次我的警报对话框出现时。

那么如何清除通知托盘点击的通知呢?

最佳答案

我也遇到了这个问题。我有这个解决方法:
我所做的是创建一个带有静态 bool 和静态方法的类:

class MessagingWidget {

static bool _isConfigured = false;


static void configuringFirebase(User currentUser, BuildContext context){


final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
if (!_isConfigured) {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");

final notification = message['data'];
if(notification['title']!=null){
if(notification['title']=="Testo"){
goToAppointmentsScreen(currentUser,context);

}
}
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");

final notification = message['data'];
if(notification['title']!=null){
if(notification['title']=="Testo"){
goToAppointmentsScreen(currentUser,context);

}
}
},
);
_isConfigured = true;
}

}


}

void goToAppointmentsScreen(User currentUser1, BuildContext context1) async {
final bool backFromAppointmentsScreen=await Navigator.push(
context1,
MaterialPageRoute(builder: (context) => Appointment(
currentUser1),
),
);
}
然后我在我的 init 中从路由小部件调用了这个方法:
@override
void initState(){
super.initState();
refreshServices();
getDirectionBasedOnLocation();
MessagingWidget.configuringFirebase(currentUser, context);
}
我希望这可以帮助你

关于firebase_messaging 如何清除通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56040993/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com