8

I have use flutter_local_notifications, push notification is successfully receiving and when i click on it, i'm trying to redirect to specific screen, but it always redirect to home screen.

Below is my code.

@override
void initState() {
  // TODO: implement initState
  super.initState();

  // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
  var initializationSettingsAndroid =
      new AndroidInitializationSettings('app_icon');
  var initializationSettingsIOS = new IOSInitializationSettings();
  var initializationSettings = new InitializationSettings(
      initializationSettingsAndroid, initializationSettingsIOS);
  flutterLocalNotificationsPlugin.initialize(initializationSettings,
      onSelectNotification: onSelectNotification);

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      print('on message $message');

      var data = message['data'] as Map;
      var msg1 = data['message'] as String;
      var response = json.decode(msg1) as Map;

      String str_title = response['title'] as String;
      String str_body = response['body'] as String;

      _showNotification(str_title, str_body, msg1);
    },
    onResume: (Map<String, dynamic> message) {
      print('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) {
      print('on launch $message');
}

_showNotification function code

void _showNotification(String title, String body, String pay_load) async {
    var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    AppConstant.notify_channel_id,
    AppConstant.notify_channel_name,
    AppConstant.notify_channel_desc,
    importance: Importance.Max,
    priority: Priority.High);
    var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
    var platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin
    .show(0, title, body, platformChannelSpecifics, payload: pay_load);
}

onSelectNotification function code

Future onSelectNotification(String payload) async {
  if (payload != null) {
    debugPrint('notification payload: ' + payload);
  }

  // here set and put condition for property id
  var response = json.decode(payload) as Map;

  Navigator.push(
    context,
    MaterialPageRoute(
        builder: (context) =>
            PropertyDetailScreen()),
);

PropertyDetailScreen is the screen where i want to redirect when click on notification, but it is always redirect to home screen, So please guide me where i am wrong, or why my code its not working.

Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24
  • Your code works for me on both Android an iOS, i tried with it with scheduled notifications, and it opens the second page on tap. Also you are missing out an `await` before `Navigator.push` as showing in the plugin's documentation (but it works without it for me anyway) so maybe its some other weird quirk. Is your payload null? does it fail to decode to json maybe? – H4CKY Dec 26 '18 at 14:32
  • @ciprianoss, should i use *scheduled notifications*?, to achieve, and payload is not null. – Nirav Bhavsar Dec 26 '18 at 14:37
  • No, i just tested with scheduled notifs so i dont have to setup a firebase project to test, does your json parsing work in onSelectNotification? – H4CKY Dec 26 '18 at 15:55
  • 1
    @ciprianoss, yes its working fine, i have debug it, and navigator push line is also executed, but not working. – Nirav Bhavsar Dec 26 '18 at 16:53
  • did find any solution? – Tushar Monirul Oct 23 '20 at 15:22

1 Answers1

3

Try by using GlobalKey as following

Declare it inside

class _MyAppState extends State<MyApp> {
    final GlobalKey<NavigatorState> navigatorKey = GlobalKey(debugLabel: "MainNavigator");
    }

Assign it in MaterialApp

MaterialApp(
              navigatorKey: navigatorKey,
              supportedLocales: [
                Locale('en', 'US'),
                Locale('ar', ''),
                Locale('it'),
                Locale('fr'),
                Locale('es'),
                Locale('de'),
                Locale('pt'),
              ],
    );

Use it as navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => YourClassName()));

Vajani Kishan
  • 293
  • 4
  • 13
  • no vaj man this wont work. Key needs to be global so it can be accessed in another class. were not working in main here – Llama Feb 22 '21 at 07:45