Flutter中关闭应用程序时如何接收本地通知?

Posted

技术标签:

【中文标题】Flutter中关闭应用程序时如何接收本地通知?【英文标题】:How to receive local notification when app is closed in Flutter? 【发布时间】:2019-04-26 17:25:58 【问题描述】:

我使用flutter_local_notifications 包。我的应用程序正常运行,并在打开时按时发送通知。但是如果它被关闭,在它打开之前不会发送任何通知。如何解决这个问题?

androidManifest.xml

<!-- The INTERNET permission is required for development. Specifically,
     flutter needs it to communicate with the running application
     to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
     calls FlutterMain.startInitialization(this); in its onCreate method.
     In most cases you can leave this as-is, but you if you want to provide
     additional functionality it is fine to subclass or reimplement
     FlutterApplication and put your custom class here. -->
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="test5"
    android:icon="@mipmap/ic_launcher">
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        <!-- This keeps the window background of the activity showing
             until Flutter renders its first frame. It can be removed if
             there is no splash screen (such as the default splash screen
             defined in @style/LaunchTheme). -->
        <meta-data
            android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
            android:value="true" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

正在初始化:

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

  @override
  initState() 
    super.initState();

    var initializationSettingsAndroid =
        new AndroidInitializationSettings('@mipmap/ic_launcher'); 
    var initializationSettingsios = new IOSInitializationSettings();
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);
  

【问题讨论】:

通过查看清单文件,它看起来是正确的。请问你是如何在你的代码上初始化它的? 我将其添加到问题中。 看起来不错。多个android版本/设备会发生这种情况吗?我一直在 iOS 和 android 上使用本地通知,没有任何问题。 @MiguelRuivo 我用版本 9 对其进行了测试,但无法正常工作。但现在我正在使用 android 版本 5 对其进行测试,并且可以正常工作。 @MiguelRuivo 在 build.gradle 中,targetSdkVersion 是 27。也许我应该把它改成 28。 【参考方案1】:

请注意确定您正在运行的插件版本,但它要求您已将应用迁移到 AndroidX,而这又需要定位到 28,例如见https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility#not-recommended-manually-migrate-your-app

【讨论】:

以上是关于Flutter中关闭应用程序时如何接收本地通知?的主要内容,如果未能解决你的问题,请参考以下文章

当我的应用在 Ionic 中关闭时如何发送通知?

当有多个具有不同通知ID的通知时,如何在android中关闭通知?

如何从通知中关闭应用程序

防止对话框在 Flutter 中关闭外部触摸

如果蓝牙在 Android 应用程序中关闭,则通知

Android:如何从活动活动中关闭前台活动?