4

当应用程序处于前台或后台时,flutter_local_notifications 工作正常

但是当应用程序终止时

onSelectNotification 没有按预期工作,而是打开应用程序

static void initializeLocalNotification() async {
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('ic_launcher');
    final IOSInitializationSettings initializationSettingsIOS =
        IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );

    final InitializationSettings initializationSettings =
        InitializationSettings(
      android: initializationSettingsAndroid,
      iOS: initializationSettingsIOS,
    );
    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String payload) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      var res = await prefs.setString(kNotificationPayLoad, payload);
      // navigatorKey.currentState
      //     .push(MaterialPageRoute(builder: (_) => LoginScreen()));
      // navigatorKey.currentState.pop();
    });

    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.requestPermissions(
          alert: true,
          badge: true,
          sound: true,
        );
  }
4

2 回答 2

9
var details = await NotificationService()
    .flutterLocalNotificationsPlugin
    .getNotificationAppLaunchDetails();
if (details.didNotificationLaunchApp) {
    print(details.payload);
}

在第一页使用此代码,它可以获得通知点击的有效负载

于 2021-05-23T02:16:21.573 回答
3

上述解决方案很好。但这对我的应用程序来说还不够。我的情况是 - 单击通知后,它将重定向到详细信息页面。前台和后台工作正常,但问题出在已终止的应用程序上。所以我确实喜欢 -

Notification_plugin.dart

Future<void> showNotification(RemoteMessage message, [bool importance = true]) async {
    dynamic notification = message.data;
    final prefs = await SharedPreferences.getInstance();

    // check message ID is valid or not
    prefs.setBool('hasMsgId', message.messageId != null ? true : false);

    await flutterLocalNotificationsPlugin.show(
      message.hashCode,
      notification['title'],
      notification['body'],
      _setPlatFormSpecificSettings(importance),
      payload: notification['docId'],
    );
}

ItemScreen.dart

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

void _runWhileAppIsTerminated() async {
    var details = await notificationPlugin.flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
    
    if (details.didNotificationLaunchApp) {
      if (details.payload != null) {
        final prefs = await SharedPreferences.getInstance();
        
        if (prefs.getBool('hasMsgId')) {
          prefs.remove('hasMsgId');
          
          if (prefs.get('authId').toString() != null) {
            Navigator.of(context).pushNamed(
              ItemDetailsScreen.routeName,
              arguments: details.payload,
            );
          } else {
            Navigator.of(context).pushReplacementNamed(AuthScreen.routeName);
          }
        }
      }
    }
}

我希望它对任何人都有帮助。谢谢!!!

于 2021-08-25T18:34:27.467 回答