Skip to content

full screen notification not closing app automaticaly in lock screen always open #2534

@vijaymsc

Description

@vijaymsc

I am implementing a new feature full-screen notification in my Flutter Android app using the flutter_local_notification plugin, whenever a notification comes in the lock screen app opens automatically in lock screen but does not close automatically always open, I need a full-screen notification app will disappear automatically the lock screen after some time. please refer below the simplified code

`
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:async';

@pragma('vm:entry-point')
Future _getFirebaseBackgroundMessaging(RemoteMessage message) async {
if (Firebase.apps.isEmpty) {
await Firebase.initializeApp();
}
await showLocalNotification(message.data['title'], message.data['body'],
isFullScreen: true);
}

@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
navigateScreen("notificationTapBackground");
}

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
final GlobalKey navigatorKey = GlobalKey();

bool _isNavigated = false;

const platform = MethodChannel(
'com.example.fullscreen_notification/lockscreen'); // Channel for lock screen check

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();

FirebaseMessaging messaging = FirebaseMessaging.instance;
await messaging.requestPermission(alert: true, sound: true, badge: true);
print("token::${await messaging.getToken()}");

const androidInitializationSettings =
AndroidInitializationSettings('@mipmap/ic_launcher');
const InitializationSettings initializationSettings =
InitializationSettings(android: androidInitializationSettings);

flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (NotificationResponse response) {
navigateScreen("onDidReceiveNotificationResponse");
},
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);

FirebaseMessaging.onMessage.listen((message) {
showLocalNotification(message.data['title'], message.data['body'],
isFullScreen: false);
});

FirebaseMessaging.onMessageOpenedApp.listen((message) {
navigateScreen("onMessageOpenedApp");
});

FirebaseMessaging.onBackgroundMessage(_getFirebaseBackgroundMessaging);

await FirebaseMessaging.instance.getInitialMessage().then((value) {});

// CHECK IF APP WAS OPENED BY NOTIFICATION
final NotificationAppLaunchDetails? launchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();

bool didNotificationLaunchApp =
launchDetails?.didNotificationLaunchApp ?? false;

if (didNotificationLaunchApp && !_isNavigated) {
navigateScreen("App launched by Notification");
}
runApp(MaterialApp(
title: 'Flutter Demo',
navigatorKey: navigatorKey,
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyApp(),
));
}

void navigateScreen(String methodName) {
Future.delayed(const Duration(milliseconds: 100), () {
if (navigatorKey.currentState?.mounted ?? false) {
_showFullScreenDialog(navigatorKey.currentState!.context);
}
});
}

void _showFullScreenDialog(BuildContext context) {
Navigator.of(context).push(MaterialPageRoute(
fullscreenDialog: true,
builder: (BuildContext context) {
Future.delayed(const Duration(seconds: 3), () {
// SystemNavigator.pop();
});
return const PaymentConformationScreen(
title: "Notification Title",
body: "Notification Body",
);
},
));
}

Future showLocalNotification(String title, String body,
{required bool isFullScreen}) async {
const int notificationId = 1001;

await flutterLocalNotificationsPlugin.show(
notificationId,
title,
body,
NotificationDetails(
android: AndroidNotificationDetails(
"full-screen-channel",
"Full Screen Notification",
fullScreenIntent: isFullScreen,
importance: Importance.high,
priority: Priority.high,
autoCancel: true,
),
),
);
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@OverRide
Widget build(BuildContext context) {
return const MyHomePage(title: 'Flutter Demo Home Page');
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;

@OverRide
State createState() => _MyHomePageState();
}

class _MyHomePageState extends State {
int _counter = 0;

void _incrementCounter() {
setState(() {
_counter++;
});
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}

class PaymentConformationScreen extends StatelessWidget {
final String title;
final String body;

const PaymentConformationScreen(
{super.key, required this.title, required this.body});

@OverRide
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.green,
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Icon(Icons.check, color: Colors.white, size: 80),
Text(title, style: const TextStyle(color: Colors.white)),
Text(body, style: const TextStyle(color: Colors.white)),
],
),
),
);
}
}

`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions