nevi_me
nevi_me

Reputation: 2730

Firebase Messaging: Infinite onLaunch Loop

I have built an Android app with Flutter, and integrated Firebase Messaging on the app.

I've been having a problem where if an Android device receives a notification while the app is closed; that notification will launch onLaunch infinitely.

I opened an issue github::flutter/flutter/issues/18524, but was hoping that someone has solved the problem (or knows the cause).

I've tried moving the code below around, but still see the same result. Even after a fresh install on a device, I still see the issue.

Has anyone come across this before?

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

  /// Navigate to item based on message type
  ///
  void _navigateToItemDetail(Map<String, dynamic> message) {
    if (message.containsKey("type")) {
      // handle messages by type
      String type = message["type"];
      String _id = message["_id"] ?? "";
      switch (type) {
        case "private_message":
          Application.router.navigateTo(context, "/private_message/$_id");
          break;
        case "announcement":
          FlutterWebBrowser.openWebPage(
            url: message["url"] ?? "https://me.app",
            androidToolbarColor: Colors.red
          );
          break;
        case "public_message":
          Application.router.navigateTo(context, "/public_message/$_id");
          break;
        default:
      }
    }
  }

  Future<Null> _showItemDialog(Map<String, dynamic> message, BuildContext ctx) async {
    return showDialog<Null>(
      context: ctx,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text(
            'New Notification!',
            style: new TextStyle(
              fontWeight: FontWeight.w800,
            )
          ),
          content: new SingleChildScrollView(
            child: new ListBody(
              children: <Widget>[
                new Text(message["summary"] ?? "You have a message"),
              ],
            ),
          ),
          actions: <Widget>[
            new FlatButton(
              textColor: Colors.red[900],
              child: new Text(
                "View",
                style: new TextStyle(fontFamily: "Roboto")
              ),
              onPressed: () {
                _navigateToItemDetail(message);
                Navigator.pop(context);
              }
            ),
            new FlatButton(
              textColor: Colors.red[900],
              child: new Text('Ignore', style: new TextStyle(fontFamily: "Roboto")),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    ).then((shs) {
      print("$shs results");
    });
  }

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      print("onMessage: $message");
      _showItemDialog(message, context);
    },
    onLaunch: (Map<String, dynamic> message) {
      print("onLaunch: $message");
      _navigateToItemDetail(message);
    },
    onResume: (Map<String, dynamic> message) {
      print("onResume: $message");
      _navigateToItemDetail(message);
    },
  );
}

Upvotes: 3

Views: 1263

Answers (1)

nevi_me
nevi_me

Reputation: 2730

The issue wasn't with Firebase Messaging.

For anyone else who finds themselves here:

I have a BottomNavigation that is based on the demo in Flutter. I was navigating to the same BottomNavigation, and that was causing an infinite navigation loop.

Upvotes: 1

Related Questions