我正在使用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
方法在 onMessage , onResume 和 onLaunch 方法方法call。
面对问题,例如我的应用程序处于后台或终止模式时,通知将来并利用通知托盘,都可以正常工作。但是,当我上另一页并回到上一个所有时间_firebaseMessaging.configure(....
方法调用时,它都有数据,因此每次我的警报对话框pups up。
那么,如何清除通知托盘单击的通知?
我也有问题。我有这样的解决方法:我所做的是用静态布尔和静态方法创建一个类:
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),
),
);
}
然后,我从路由小部件的初始化中调用了此方法:
@override
void initState(){
super.initState();
refreshServices();
getDirectionBasedOnLocation();
MessagingWidget.configuringFirebase(currentUser, context);
}
我希望这对您有帮助
我知道,这有点丑陋,但我只知道:
- 添加flutter_local_notifications
-
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
-
只需创建
final fln = FlutterLocalNotificationsPlugin();
-
并在需要时使用
fln.cancelAll()
。
我为防止OnLaunch和OnRemume方法一次运行的方法是一次又一次运行的是使用最后一个通知(我使用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');
}
}
这是另一个解决方案,使用 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);
},
);
尝试在initstate中而不是在自定义方法中配置firebasemessging。应该有效:(