5

I was trying to use the firebase cloud assessing and in the process, I want to just show a popup dialogue to the user on the arrival of the push notification. But to show the dialogue we need thee context object as one of the arguments of showDialog is BuildContext.

I tried many approaches but that didn't work. As of now, my code looks likes this:

_firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) {
          print('onMessage: $message');
          return;
        },
        onBackgroundMessage: myBackgroundMessageHandler,
        onResume: (Map<String, dynamic> message) {
          print('onResume: $message');
          return;
        },
        onLaunch: (Map<String, dynamic> message) {
          print('onLaunch: $message');
            Text('onLaunch: $message'),
          );
          return;
        });

Note: This code is written in a separate class, and I am trying to achieve it without any 3rd part library.

Deepak Kumar
  • 1,246
  • 14
  • 38

2 Answers2

1

First, you can't show a dialog without a valid context. Why don't you simply pass a BuildContext to your class like this?

class SeparateClass {
  final BuildContext context;

  SeparateClass(this.context);

  void configure() {
    // your rest of the configuration code
    // you can use showDialog(context, ...) here
  }
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • I tried this too, it's not working for me. I called this config method of `NotificationManager` from the build method of main.dart by passing BuildContext object. Still, it's not showing the popup. – Deepak Kumar May 07 '20 at 16:32
  • 1
    @DKAnsh Please post your full code, if it is reproducible I can assist you further. – CopsOnRoad May 08 '20 at 00:37
0

i solved a similar problem that shows a SnackBar instead a Dialog. With this code you can launch a SnackBar or Dialog wherever you want in your application.

import 'package:collectio/pages/init_screen_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'helper.dart';

void main() {
  Provider.debugCheckInvalidValueType = null;
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      // here the context refers to MaterialApp widget,
      // you can´t call Scaffold.of(context)
        body: Builder(builder: (context) {
          // here the context refers to Scaffold widget
          Helper.startFirebaseMessaging(context);
          return InitScreenPage();
        }),
    ));
  }
}

class Helper {
  static startFirebaseMessaging(BuildContext buildContext) {
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        var notification = message['notification'];

        // build the snackbar
        final snackBar = SnackBar(
          content: Text(notification['title']),
          action: SnackBarAction(
              label: 'Ok',
              onPressed: (){}
          ),
        );

        try {
          Scaffold.of(buildContext).showSnackBar(snackBar);
        } catch (e) {
          print(e);
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      print("Push Messaging token: $token");;
    });
    print("Waiting for token...");
  }
}

Maybe you can apply this to your code.

teteArg
  • 3,684
  • 2
  • 20
  • 18