flutter local notification not works properly when the app is terminated

Viewed 832

I've run a simple example on my android device. When the app is terminated and I click on the appeared notification, the app runs but it doesn't receive any payload. In another word, I see the app's main UI and it doesn't show the AlertDialog('Hello Everyone').

My code is as follows:

    import 'package:flutter/material.dart';
    import 'package:flutter_local_notifications/flutter_local_notifications.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          title: 'Flutter Demo',
          theme: ThemeData(
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Local Notifications'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      FlutterLocalNotificationsPlugin flutterNotificationPlugin;
    
      @override
      void initState() {
        var initializationSettingsAndroid =
            new AndroidInitializationSettings('app_icon');
    
        var initializationSettingsIOS = new IOSInitializationSettings();
    
        var initializationSettings = new InitializationSettings(
            android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    
        flutterNotificationPlugin = FlutterLocalNotificationsPlugin();
    
        flutterNotificationPlugin.initialize(initializationSettings,
            onSelectNotification: onSelectNotification);
      }
    
      Future onSelectNotification(String payload) async {
        showDialog(
            context: context,
            builder: (_) => AlertDialog(
                  title: Text("Hello Everyone"),
                  content: Text("$payload"),
                ));
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
              child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              TextButton(
                child: Text("Notification with Default Sound"),
                onPressed: () {
                  notificationDefaultSound();
                },
              ),
            ],
          )),
        );
      }
    
      Future notificationDefaultSound() async {
        var androidPlatformChannelSpecifics = AndroidNotificationDetails(
          'Notification Channel ID',
          'Channel Name',
          channelDescription: 'Description',
          importance: Importance.max,
          priority: Priority.high,
        );
    
        var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    
        var platformChannelSpecifics = NotificationDetails(
            android: androidPlatformChannelSpecifics,
            iOS: iOSPlatformChannelSpecifics);
    
        flutterNotificationPlugin.show(0, 'New Alert',
            'How to show Local Notification', platformChannelSpecifics,
            payload: 'Default Sound');
      }
    }

The packages are as below:

  cupertino_icons: ^1.0.1
  flutter_local_notifications: ^9.0.0

Anybody can help me to solve my problem in android devices, please?

1 Answers

According to the plugin docs, some Android OEMs have their own customised Android OS that can prevent applications from running in the background. Consequently, scheduled notifications may not work when the application is in the background on certain devices (e.g. by Xiaomi, Huawei).

Related