firebasemessage显示多个通知
我阅读了Stackoverflow上的主题,但没有找到答案。一切都可以通过通知正常,但是问题是,如果我调用通知发送超过1次的函数,则每次发送超过1个通知。例如,如果我单击列表索引以发送某些令牌的通知,则它会发送正常,但是如果我再次单击,它将发送2次,如果我再次单击,它将发送3次,等等...我该如何解决此问题?
sendNotificationAndroid('Example of title', token);
sendNotificationAndroid(String title, String token)async{
final data = {
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
'message': title,
};
try{
http.Response response = await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),headers: <String,String>{
'Content-Type': 'application/json',
'Authorization': 'key=XXXXXXXXXX'
},
body: jsonEncode(<String,dynamic>{
'notification': <String,dynamic> {'title': title,'body': 'Example'},
'priority': 'high',
'data': data,
'to': '$token'
})
);
if(response.statusCode == 200){
print("Yeh notificatin is sended");
}else{
print("Error");
}
}catch(e){
}
}
OBS:我在侦听中调用函数sendnotificationandroid(流构建器中的List Tile的插件);
initState(){
super.initState();
var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings = InitializationSettings(android: initializationSettingsAndroid,);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
FirebaseMessaging.onMessage.listen((event) {
LocalNotificationService.display(event);
});
}
I read topics on stackoverflow and i didn't found the answer. Everything works fine with notifications, but the problem is that if I call the function that send notification more than 1 time it sends more than 1 notification per time. For example, if I click on the list index to send notification for certain token it sends normal but if I click again it sends 2 times and if I click again it sends 3 times etc... How can I solve this problem?
sendNotificationAndroid('Example of title', token);
sendNotificationAndroid(String title, String token)async{
final data = {
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done',
'message': title,
};
try{
http.Response response = await http.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),headers: <String,String>{
'Content-Type': 'application/json',
'Authorization': 'key=XXXXXXXXXX'
},
body: jsonEncode(<String,dynamic>{
'notification': <String,dynamic> {'title': title,'body': 'Example'},
'priority': 'high',
'data': data,
'to': '$token'
})
);
if(response.statusCode == 200){
print("Yeh notificatin is sended");
}else{
print("Error");
}
}catch(e){
}
}
OBS: I'm calling the function sendNotificationAndroid inside a listen (OnTap of List tile in a Stream builder);
initState(){
super.initState();
var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettings = InitializationSettings(android: initializationSettingsAndroid,);
flutterLocalNotificationsPlugin.initialize(initializationSettings);
FirebaseMessaging.onMessage.listen((event) {
LocalNotificationService.display(event);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次
initstate
被称为新侦听器。firebasemessaging.onmessage.listen
应调用一次。Every time
initState
is called a new listener is added.FirebaseMessaging.onMessage.listen
should be called once.这个问题很可能是由于您没有在安排新的通知之前取消先前计划的通知。要解决此问题,您应该在安排新的问题之前取消先前计划的通知。
当您安排通知时,您可以通过跟踪Flutter_Local_notifications返回的通知ID来实现这一目标。然后,当您要发送新的通知时,可以使用FlutterLocalnotificationplugin类取消以前计划的通知。
这是一个示例代码段,演示了如何实现这一目标:
}
This issue is most likely caused by the fact that you are not canceling the previously scheduled notifications before scheduling new ones. To solve this problem, you should cancel the previously scheduled notifications before scheduling new ones.
You can achieve this by keeping track of the notification ID that is returned by the flutter_local_notifications package when you schedule a notification. Then, when you want to send a new notification, you can first cancel the previously scheduled notification with that ID using the cancel method of the FlutterLocalNotificationsPlugin class.
Here's an example code snippet that demonstrates how you can achieve this:
}
在显示通知之前,您可以取消以前的通知
或通过ID。
Before showing notification you can cancel previous notification
or by id.