Local Notification always open the MaterialApp home Screen when closed

Viewed 87

I am implementing flutter_local_notifications and scenarios where user is still inside the app and exits the app by simple pressing home button work fine, user can still go to the desired screen from an notification, but when the user exits by pressing back button (android) clicking the notifications sends user to the home defined screen.

main.dart

import 'package:flutter/material.dart';
import 'package:local_demo/notification_manager.dart';
import 'package:local_demo/screen1.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await NotificationsManager().initializeNotifications();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      navigatorKey: NotificationsManager.navigatorKey,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Screen1(),
    );
  }
}

notifications_manager.dart

import 'dart:developer';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:local_demo/screen2.dart';

class NotificationsManager {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  Future<void> initializeNotifications() async {
    AndroidInitializationSettings initializationSettingsAndroid =
        const AndroidInitializationSettings('ic_launcher');

    const IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings();

    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: initializationSettingsAndroid,
            iOS: initializationSettingsIOS);
    log('NotificationsManager.dart::: notification!!!');
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  }

  shootNotification(String title, String body, String payload, context) async {
    // var flutterLocalNotificationsPlugin =
    //     HealthCareApp.getNotificationsPlugin(context);

    var androidNotificationDetails = const AndroidNotificationDetails(
      '001',
      'data',
      category: 'information',
      importance: Importance.high,
    );
    var iOSNotificationDetails = IOSNotificationDetails();
    var generalNotificationDetails = NotificationDetails(
        android: androidNotificationDetails, iOS: iOSNotificationDetails);
    await flutterLocalNotificationsPlugin
        .show(002, title, body, generalNotificationDetails, payload: payload);
  }

  showNotification(String? payload) {
    navigatorKey.currentState!
        .push(MaterialPageRoute(builder: (context) => Screen2(payload!)));
  }

  void onSelectNotification(String? payload) async {
    log('main.dart::: notification arrived!!!');

    if (payload != null) {
      log('notification payload: $payload');
    }

    // print('payloadReference: $payload');
    // BuildContext _context = navigatorKey.currentContext;

    showNotification(payload!);
  }
}

screen1.dart

import 'package:flutter/material.dart';
import 'package:local_demo/notification_manager.dart';

class Screen1 extends StatelessWidget {
  const Screen1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Screen1')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            NotificationsManager()
                .shootNotification('title', 'body', 'pay111', context);
          },
          child: const Text('Shoot Notification'),
        ),
      ),
    );
  }
}

screen2.dart

import 'package:flutter/material.dart';

class Screen2 extends StatelessWidget {
  String notificationContent;

  Screen2(this.notificationContent, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Screen2')),
      body: Center(
        child: Text(notificationContent),
      ),
    );
  }
}
1 Answers

replace push with pushNamed and set RouteSetting In MyApp Widget replace Screen1 at home with variable init it with Screen1 like this var route ='Screen1' home=route variable route should be global variable so you can edit it so put it in sparete file you can access it from everywhere then on shownotification edit it route ='screen2'

 App.navigatorKey.currentState.pushNamed(
      route,
    );
        
Related