Flutter如何打开特定的屏幕上点击预定通知

pbossiut  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(129)

我用两种方式在我的应用程序中发送通知。当我通过FireBase发送通知时,我的通知单击操作起作用。当我通过FlutterLocalNotificationsPlugin.zonedSchedule发送通知时,会收到通知,但点击时什么也没有发生。如何在计划通知上设置单击操作?

// my schedule notifications 

Future scheduleNotification(
      {int id = 0,
      String? title,
      String? body,
      String? payLoad,
      required DateTime scheduledNotificationDateTime}) async {
    return notificationsPlugin.zonedSchedule(
        id,
        title,
        body,
        tz.TZDateTime.from(
          scheduledNotificationDateTime,
          tz.local,
        ),
        const NotificationDetails(
          iOS: DarwinNotificationDetails(
            presentAlert: true,
            presentBadge: true,
            presentSound: true,
          ),
            android: AndroidNotificationDetails(
                'your channel id', 'your channel name',
                importance: Importance.max,
                priority: Priority.high,
                icon: '@mipmap/ic_launcher',
                showWhen: false)),
        androidAllowWhileIdle: true,
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime);
  }

// my notification service
class NotificationService {
  final FlutterLocalNotificationsPlugin notificationsPlugin =
  FlutterLocalNotificationsPlugin();
  AndroidNotificationChannel channel = const AndroidNotificationChannel(
    'high_importance_channel',
    'High Importance Notifications',
    description: 'This channel is used for important notifications.',
    importance: Importance.high,
  );

  Future<void> initNotifications() async {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );

    FirebaseMessaging messaging = FirebaseMessaging.instance;
    RemoteMessage? initialMessage =
    await FirebaseMessaging.instance.getInitialMessage();
    if (initialMessage != null) {
      print(initialMessage.data);
      if (initialMessage.data['invoice_id'] != null) {
        Future.delayed(const Duration(milliseconds: 1250))
            .then((value) =>
            navKey.currentState!.push(
              MaterialPageRoute(
                  builder: (context) =>
                      WareHouseSalesDetails(
                        id: int.parse(initialMessage.data['id']),
                        invoiceId: '${initialMessage.data['invoice_id']}',
                      )),
            ));
      }
      if (initialMessage.data['moving_id'] != null) {
        Future.delayed(const Duration(milliseconds: 1250))
            .then((value) =>
            navKey.currentState!.push(
              MaterialPageRoute(
                  builder: (context) =>
                      MovingDetailsPage(
                        id: int.parse(initialMessage.data['id']),
                        movingId: '${initialMessage.data['moving_id']}',
                      )),
            ));
      }
    }

    await messaging.setForegroundNotificationPresentationOptions(
      alert: true,
      badge: true,
      sound: true,
    );

    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification!.android;
      if (notification != null && android != null) {
        notificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              channelDescription: channel.description,
              icon: '@mipmap/ic_launcher',
            ),
          ),
        );
      }
      print(message!.data);
      if (message.data['invoice_id'] != null) {
        navKey.currentState!.push(
          MaterialPageRoute(
              builder: (context) =>
                  WareHouseSalesDetails(
                    id: int.parse(message.data['id']),
                    invoiceId: '${message.data['invoice_id']}',
                  )),
        );
      }
      if (message.data['moving_id'] != null) {
        navKey.currentState!.push(
          MaterialPageRoute(
              builder: (context) =>
                  MovingDetailsPage(
                    id: int.parse(message.data['id']),
                    movingId: '${message.data['moving_id']}',
                  )),
        );
      }
    });

    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage? message) async {
      print(message!.data);
      if (message!.data['invoice_id'] != null) {
        navKey.currentState!.push(
          MaterialPageRoute(
              builder: (context) =>
                  WareHouseSalesDetails(
                    id: int.parse(message.data['id']),
                    invoiceId: '${message.data['invoice_id']}',
                  )),
        );
      }
      if (message.data['moving_id'] != null) {
        navKey.currentState!.push(
          MaterialPageRoute(
              builder: (context) =>
                  MovingDetailsPage(
                    id: int.parse(message.data['id']),
                    movingId: '${message.data['moving_id']}',
                  )),
        );
      }
    });

    var initializationSettingsAndroid =
    const AndroidInitializationSettings('@drawable/ic_notification');
    var initializationSettingsIOS = const DarwinInitializationSettings();
    var initializationSettings = InitializationSettings(
        android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    await messaging.requestPermission();
    if (Platform.isIOS) {
      var APNS = await messaging.getAPNSToken();
    }
    FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
  }
}

字符串

pod7payv

pod7payv1#

你要找的是payload。你可以像这样将它添加到zonedSchedule函数中:

flutterLocalNotificationsPlugin.zonedSchedule(id, title, body, scheduledDate, notificationDetails, payload: 'my_payload');

字符串
然后,您可以通过将onDidReceiveLocalNotification方法添加到flutterLocalNotificationsPlugin.initialize中来处理负载。在那里,你可以从细节中获取有效负载,并对它做任何你想做的事情,例如:

void initalizeNotifications() {
    // ...
    await flutterLocalNotificationsPlugin.initialize(initializationSettings,
    onDidReceiveNotificationResponse: onDidReceiveNotificationResponse);
}
void onDidReceiveNotificationResponse(NotificationResponse notificationResponse) {
    Navigator.push(
      context,
      MaterialPageRoute<void>(builder: (context) => SecondScreen(payload)),
    );
}

相关问题