簡體   English   中英

firebase_messaging 如何清除通知?

[英]firebase_messaging how to clear notification?

我正在使用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方法將在每次onMessageonResumeonLaunch方法調用時調用。

面臨諸如當我的應用程序處於后台或終止模式時會出現通知並將點擊通知托盤的問題,一切都很好。 但是,當我轉到其他頁面並一直返回到前一個頁面時, _firebaseMessaging.configure(....方法調用及其具有數據,因此每次我的警報對話框出現時。

那么如何清除通知托盤點擊的通知呢?

我也遇到了這個問題。 我有這個解決方法:我所做的是創建一個帶有靜態 bool 和靜態方法的類:

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

然后我在我的 init 中從路由小部件調用了這個方法:

@override
  void initState(){
    super.initState();
    refreshServices();
    getDirectionBasedOnLocation();
    MessagingWidget.configuringFirebase(currentUser, context);
}

我希望這可以幫助你

為了防止 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');
  }
}

這是另一個解決方案,使用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 而不是自定義方法中配置 firebaseMessaging。 那應該工作:)

我知道,這有點難看,但我只知道這樣:

  1. 添加flutter_local_notifications
  2. import 'package:flutter_local_notifications/flutter_local_notifications.dart';
  1. 只需創建final fln = FlutterLocalNotificationsPlugin();

  2. 並在需要時使用fln.cancelAll()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM