FirebaseMessage 显示多个通知

问题描述 投票:0回答:2

我阅读了有关 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(Stream 构建器中的 List tile 的 OnTap);

initState(){
    super.initState();
  var initializationSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
  var initializationSettings = InitializationSettings(android: initializationSettingsAndroid,);
  flutterLocalNotificationsPlugin.initialize(initializationSettings);
    FirebaseMessaging.onMessage.listen((event) {
      LocalNotificationService.display(event);
    });
  }
flutter firebase firebase-cloud-messaging message
2个回答
0
投票

每次调用

initState
时都会添加一个新的监听器。
FirebaseMessaging.onMessage.listen
应该被调用一次。


0
投票

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

您可以通过在安排通知时跟踪 flutter_local_notifications 包返回的通知 ID 来实现此目的。然后,当你想发送新的通知时,你可以先使用 FlutterLocalNotificationsPlugin 类的 cancel 方法取消之前用该 ID 安排的通知。

这里有一个示例代码片段,演示了如何实现这一点:

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);

}

© www.soinside.com 2019 - 2024. All rights reserved.