简体   繁体   English

应用程序进入后台时在 Flutter 上显示本地通知

[英]Display Local Notifications on Flutter the moment the App goes to the background

For some weird reasons I can't get notifications displayed on my flutter app the moment the app is exited.由于某些奇怪的原因,我的 flutter 应用程序退出时无法收到通知。

What I want is that the moment the user exists the app I want to display a notification to tell the user that the app has currently gone into the background.我想要的是,当用户存在应用程序时,我想显示一条通知,告诉用户该应用程序当前已进入后台。

I am currently using the flutter_local_notifications plugin to achieve this but it isn't working.我目前正在使用flutter_local_notifications插件来实现这一点,但它不起作用。

Here is what I have tried:这是我尝试过的:

class HomePage extends StatefulWidget {
  static const routePath = "/home";

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage > with WidgetsBindingObserver {

  @override
  initState() {
    WidgetsBinding.instance!.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance!.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    bool inBackground = state == AppLifecycleState.paused;
    if (inBackground) {
displayNotification("AppName","App has gone into the background",null); //This is not displaying at all when the app goes into the background. It only displays when the app is in the foreground. I want it to display the moment the user exits the app.
    }
  }

Future<void> displayNotification(
  String title,
  String description,
  dynamic payload, {
  bool ongoing = false,
}) async {
.....

Any insights to get around this would be really appreciated.任何解决此问题的见解将不胜感激。

I test this and worked for me我对此进行了测试并为我工作

main.dart : main.dart

import 'package:flutter/material.dart';
import 'package:local_notification/local_notification.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance?.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    var appInBackground = (state == AppLifecycleState.inactive);
    if (appInBackground)
      showNotificationMessage('App has gone into the background', 'FlutterApp');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Text(
          'Testing app local notification\r\n'
          ' in background mode!',
          style: TextStyle(fontSize: 28),
        ),
      ),
    );
  }
}

local_notification.dart : local_notification.dart

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

//define this method in global scope not a class scope
void showNotificationMessage(String? description, String? title) {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
  var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
  var iOS = new IOSInitializationSettings();
  var initSettings = new InitializationSettings(android: android, iOS: iOS);
  flutterLocalNotificationsPlugin.initialize(initSettings,
      onSelectNotification: null);

  String groupKey = 'com.example.general-notification-channel';
  var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
    'general-notification-channel',
    'general-notification-channel',
    'general-notification-channel',
    importance: Importance.max,
    priority: Priority.high,
    groupKey: groupKey,
    //   setAsGroupSummary: true
  );

  var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
  NotificationDetails platformChannelSpecifics = new NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics);
  flutterLocalNotificationsPlugin.show(
      0, title, description, platformChannelSpecifics);
}

pubspec.yaml : pubspec.yaml

name: local_notification
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1

environment:
  sdk: ">=2.12.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.3
  flutter_local_notifications: ^6.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter


flutter:
  uses-material-design: true

try running your app with release mode using flutter run --release and check because in debug mode app will force to kill when we exit the app尝试使用 flutter 以发布模式运行您的应用程序运行 --release 并检查,因为在调试模式下,当我们退出应用程序时应用程序将强制终止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 本地通知 - 当应用程序在后台时 ZonedSchedule 通知不起作用 - Flutter Local notifications - ZonedSchedule notification is not working while the app in background 在flutter中在后台更新的预定本地通知 - scheduled local notifications that update in the background in flutter 如何在Flutter应用程序上使用iOS显示本地通知? - How to display local notifications with iOS on Flutter application? Flutter:本地通知更改应用程序名称 - Flutter: Local Notifications changing name of the app Flutter:应用程序终止时,flutter_local_notifications onSelectNotification 不起作用 - Flutter : flutter_local_notifications onSelectNotification not working when app is terninated firebase 和 flutter 本地通知处理后台通知 - both firebase and flutter local notifications handle background notificaiton 当应用程序进入后台时,Flutter CamerPlugin“相机预览”冻结 - Flutter CamerPlugin “camera preview” freezed when app goes to background 如何使用flutter插件显示多个通知:flutter_local_notifications - How to display multiple notifications using flutter plugin : flutter_local_notifications 无法从我的 flutter 应用程序发送本地通知 - Unable to send local notifications from my flutter app Flutter 本地通知不执行应用程序终止的代码 - Flutter Local Notifications doesn't execute code with app terminated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM