flutter_local_notifications 的 onSelectNotification 方法在 Android 的页面开始时立即调用(在 iOS 中没有这样的问题)

onSelectNotification method of flutter_local_notifications called immediately on page start in Android (in iOS no such issue)

在我的应用程序中,我使用 flutter_local_notifications 插件和 onSelectNotification 方法,当页面启动时 Android 立即触发,即使您没有点击通知。

这是我的 initState 的样子:

 @override
  void initState() {
    super.initState();

flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final android = AndroidInitializationSettings('@mipmap/ic_launcher');
final iOS = IOSInitializationSettings();
final initSettings = InitializationSettings(android: android, iOS: iOS);

flutterLocalNotificationsPlugin.initialize(initSettings,
    onSelectNotification: _onSelectNotification);

 }

我的 _onSelectNotification 通知位于构建方法之外

在 iOS 中一切正常

我刚刚添加了一个标志来处理这个案例:

bool flagDueNotificationIssue = true; //called once when page opened for the first time

_onSelectNotification(){  
   if (Platform.isAndroid && flagDueNotificationIssue) {
      flagDueNotificationIssue = false;
      return;
    }

.... task to do when notification clicked
}

所以当这个函数第一次触发时它不会起作用(即它有助于解决这个函数的第一次假调用),在其他情况下,它会按预期起作用。