firebasemessage显示多个通知

发布于 2025-02-04 10:22:50 字数 1598 浏览 4 评论 0原文

我阅读了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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单挑你×的.吻 2025-02-11 10:22:50

每次initstate被称为新侦听器。 firebasemessaging.onmessage.listen应调用一次。

Every time initState is called a new listener is added. FirebaseMessaging.onMessage.listen should be called once.

瞳孔里扚悲伤 2025-02-11 10:22:50

这个问题很可能是由于您没有在安排新的通知之前取消先前计划的通知。要解决此问题,您应该在安排新的问题之前取消先前计划的通知。

当您安排通知时,您可以通过跟踪Flutter_Local_notifications返回的通知ID来实现这一目标。然后,当您要发送新的通知时,可以使用FlutterLocalnotificationplugin类取消以前计划的通知。

这是一个示例代码段,演示了如何实现这一目标:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

// Keep track of the notification IDs
int notificationId = 0;

// Schedule a notification
Future<void> scheduleNotification(String token) async {
// Cancel the previously scheduled notification, if any
await flutterLocalNotificationsPlugin.cancel(notificationId);

// Increment the notification ID
notificationId++;

// Construct the notification details
final AndroidNotificationDetails androidPlatformChannelSpecifics =
  AndroidNotificationDetails(
      'your_channel_id', 'your_channel_name', 'your_channel_description',
      importance: Importance.max, priority: Priority.high, showWhen: false);
final NotificationDetails platformChannelSpecifics =
  NotificationDetails(android: androidPlatformChannelSpecifics);

// Schedule the notification
await flutterLocalNotificationsPlugin.show(
  notificationId,
  'Notification title',
  'Notification body',
  platformChannelSpecifics,
  payload: token);

}

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:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

// Keep track of the notification IDs
int notificationId = 0;

// Schedule a notification
Future<void> scheduleNotification(String token) async {
// Cancel the previously scheduled notification, if any
await flutterLocalNotificationsPlugin.cancel(notificationId);

// Increment the notification ID
notificationId++;

// Construct the notification details
final AndroidNotificationDetails androidPlatformChannelSpecifics =
  AndroidNotificationDetails(
      'your_channel_id', 'your_channel_name', 'your_channel_description',
      importance: Importance.max, priority: Priority.high, showWhen: false);
final NotificationDetails platformChannelSpecifics =
  NotificationDetails(android: androidPlatformChannelSpecifics);

// Schedule the notification
await flutterLocalNotificationsPlugin.show(
  notificationId,
  'Notification title',
  'Notification body',
  platformChannelSpecifics,
  payload: token);

}

生死何惧 2025-02-11 10:22:50

在显示通知之前,您可以取消以前的通知

  await flutterLocalNotificationsPlugin.cancelAll(); 

或通过ID。

Before showing notification you can cancel previous notification

  await flutterLocalNotificationsPlugin.cancelAll(); 

or by id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文