簡體   English   中英

當通知到達時,如何在任何屏幕中顯示自定義對話框?

[英]How to show a custom dialog in any screen in when notification arrived flutter?

當推送通知到達我的應用程序時,我試圖在任何活動屏幕中顯示一個對話框。 在應用程序運行時。 我可以通過用戶交互顯示對話框,例如單擊按鈕。 但我想在沒有用戶交互的情況下展示它。 如果有通知到達,則僅應觸發對話框。 我試圖用后台獲取來調用它。 但找不到任何解決方案。 因此,請提前幫助並感謝您。

我以前遇到過同樣的問題,我會展示我的解決方案,希望它適合你

解決方案的主要內容:當應用程序像 HomePage 一樣生活時,我們的頁面不會從導航器堆棧中彈出,因此我們可以使用此頁面中的 BuildContext

因此,通過將必須仍在 Navigator 堆棧中的 StatefulWidget(如主​​頁)的上下文(當應用程序處於活動狀態時不彈出它)傳遞給處理通知數據的類,您可以使用它來顯示對話框

現在讓我們寫一些代碼:

例如,我們有NotificationManger類,此類用於使用靜態方法處理通知消息

class NotificationManger {
    
   static BuildContext _context;


    
   static init({@required BuildContext context}) {
    _context = context;
    
  }

 //this method used when notification come and app is closed or in background and 
 // user click on it, i will left it empty for you
 static handleDataMsg(Map<String, dynamic> data){

 }

 //this our method called when notification come and app is foreground
 static handleNotificationMsg(Map<String, dynamic> message) {
    debugPrint("from mangger  $message");

    final dynamic data = message['data'];
    //as ex we have some data json for every notification to know how to handle that
    //let say showDialog here so fire some action 
    if (data.containsKey('showDialog')) {
      // Handle data message with dialog
      _showDialog(data);
    }
  }


   static _showDialog({@required Map<String, dynamic> data}) {

    
        //you can use data map also to know what must show in MyDialog
        showDialog(context: _context,builder: (_) =>MyDialog());


  }

}

現在我們在我的應用程序的FCM類中將此回調作為頂級或靜態(必須是其中之一)

class Fcm {
  static final FirebaseMessaging _fcm = FirebaseMessaging();

  static initConfigure() {
    if (Platform.isIOS) _iosPermission();

    _fcm.requestNotificationPermissions();
    _fcm.autoInitEnabled();

    _fcm.configure(
        onMessage: (Map<String, dynamic> message) async =>
            NotificationManger.handleNotificationMsg(message),
        onLaunch: (Map<String, dynamic> message) async =>
            NotificationManger.handleDataMsg(message['data']),
        onResume: (Map<String, dynamic> message) async =>
            NotificationManger.handleDataMsg(message['data']),
        onBackgroundMessage: async =>
            NotificationManger.handleDataMsg(message['data']);


  }

  static _iosPermission() {
    _fcm.requestNotificationPermissions(
        IosNotificationSettings(sound: true, badge: true, alert: true));
    _fcm.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
  }

}

要了解有關回調 fcm 的更多信息,請閱讀 此內容

好的,現在在我們的HomePage State 中,在initState方法中初始化我們的類

 @override
  void initState() {
    super.initState();
  

    Future.delayed(Duration.zero,(){
      ///init Notification Manger
      NotificationManger.init(context: context);

      ///init FCM Configure
      Fcm.initConfigure();

     
    });


  }

如前所述,當應用程序顯示時主頁不會彈出,您可以啟動另一個頁面但不關閉主頁

我希望這有幫助

暫無
暫無

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

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