我正在使用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);
},
);
}
每次调用 onMessage , onResume 和 onLaunch 方法时,将在_showPushNotificationDialog
方法处调用。
面临的问题,例如当我的应用程序处于后台或终止模式时,通知将出现,并轻按通知托盘,一切正常。但是,当我转到另一页并一直返回到上一个_firebaseMessaging.configure(....
方法调用时,该方法调用具有数据,因此每次我的警报对话框都弹出。
那么如何清除通知托盘中单击的通知?
答案 0 :(得分:0)
尝试在initState而非自定义方法中配置firebaseMessaging。那应该工作:)
答案 1 :(得分:0)
我也有这个问题。我有以下解决方法: 我所做的就是用静态布尔和静态方法创建一个类:
Int
然后我在路由小部件的初始化中调用了此方法:
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),
),
);
}
希望对您有帮助
答案 2 :(得分:0)
我知道,这有点丑陋,但我只有这样知道:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
只需创建final fln = FlutterLocalNotificationsPlugin();
,并在需要时使用fln.cancelAll()
。
答案 3 :(得分:0)
为了防止 onLaunch 和 onResume 方法一次又一次地运行,我所做的是用最后一个通知检查当前通知(我使用 shared_preferences)
这是片段:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
onMessageReceived(context, message);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
onLaunch(context, message);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
onLaunch(context, message);
},
);
.
.
.
void onLaunch(BuildContext context, Map<String, dynamic> remoteMessage) async {
var pref = SharedPreferences.getInstance();
var data = remoteMessage['data'] ?? remoteMessage;
String lastData = '';
await pref.then((prefs) {
lastData = prefs.get('remote_message');
});
if ((data['type'] != null || data['id'] != null) &&
data.toString() != lastData) {
toDetailPageFromPush(
context,
data['type'],
data['id'],
);
pref.then((prefs) {
prefs.setString('remote_message', data.toString());
});
} else {
print('on launch error $remoteMessage');
}
}
答案 4 :(得分:0)
这是另一种解决方案,使用通知中的唯一值 message_id,因此我们可以使用共享首选项保存通知的最后一个 id 并与当前通知进行比较:
processNotification(message, BuildContext context) async {
try {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String lastMessageId = sharedPreferences.getString('last_message_id');
String currentMessageId = message['data']['google.message_id'];
//COMPARE NOTIFICATIONS ID
if(currentMessageId != lastMessageId) {
//SET LAST NOTIFICATION ID
sharedPreferences.setString('last_message_id', currentMessageId);
//SHOW A DIALOG OR NAVIGATE TO SOME VIEW, IN THIS CASE TO A VIEW
String screen = message['data']['screen'];
Navigator.of(context).pushNamed(screen);
}
} catch (e) {
print('ERROR PROCESSING NOTIFICATION');
print(e);
}
}
现在我们可以在配置中调用这个函数了:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
processNotification(context,message);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
processNotification(context,message);
},
onLaunch: (Map<String, dynamic> message) async {
processNotification(context,message);
},
);