ios firebase getInitialMessage不工作

发布于 2025-02-13 01:45:11 字数 4542 浏览 0 评论 0原文

我在扑通项目中安装了Firebase通知包,并设置了发送数据的必要设置。

一切正常在Android上正常工作,当应用程序完全关闭时,当我通过Firebase发送参数时,我想要打开的页面,但是在iOS部分,当我单击通知时,它不会捕获单击和页面i。想要不打开。

如果您能帮助我,我将非常高兴的是,我应该关注iOS部分的要点。

main.dart

  initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  if (initialMessage != null) {
    if (initialMessage!.data['type'] == 'notification') {
      await Future.delayed(const Duration(seconds: 4));
      navigatorKey.currentState?.pushNamed(
          '/notification'); // navigate to login, with null-aware check

    }
  } 

pushNotificationservice.dart

class PushNotificationService {

  Future<void> setupInteractedMessage() async {
    await Firebase.initializeApp();

    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();
 
    if (initialMessage != null &&
        initialMessage.data['type'] == 'notification') {
      await Future.delayed(const Duration(seconds: 6));
      navigatorKey.currentState?.pushNamed(
          '/notification'); // navigate to login, with null-aware check

    }
// Also handle any interaction when the app is in the background via a
    // Stream listener
    // This function is called when the app is in the background and user clicks on the notification
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      // Get.toNamed(NOTIFICATIOINS_ROUTE);
      print(message.toMap().toString());
      print(message.data['type'].toString());
      if (message.data['type'] == 'notification') {
        navigatorKey.currentState?.pushNamed('/notification');
      }
    });
    await enableIOSNotifications();
    await registerNotificationListeners();
  }

  registerNotificationListeners() async {
    AndroidNotificationChannel channel = androidNotificationChannel();
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
    var androidSettings =
        const AndroidInitializationSettings('@mipmap/ic_launcher');
    var iOSSettings = const IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );
    var initSetttings =
        InitializationSettings(android: androidSettings, iOS: iOSSettings);
    flutterLocalNotificationsPlugin.initialize(initSetttings,
        onSelectNotification: (message) async {
      // This function handles the click in the notification when the app is in foreground
      // Get.toNamed(NOTIFICATIOINS_ROUTE);
    });
// onMessage is called when the app is in foreground and a notification is received
    FirebaseMessaging.onMessage.listen((RemoteMessage? message) {
      // Get.find<HomeController>().getNotificationsNumber();
      print(message!.data['type'].toString());
      print(message.toMap().toString());
      if (message.data['type'] == 'notification') {
        navigatorKey.currentState?.pushNamed('/notification');
      }
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
      // local notification to show to users using the created channel.
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              icon: android.smallIcon,
              playSound: true,
            ),
          ),
        );
      }
    });
  }

  enableIOSNotifications() async {
    await FirebaseMessaging.instance
        .setForegroundNotificationPresentationOptions(
      alert: true, // Required to display a heads up notification
      badge: true,
      sound: true,
    );
  }

  androidNotificationChannel() => const AndroidNotificationChannel(
        'high_importance_channel', // id
        'High Importance Notifications', // title
        importance: Importance.max,
      );
}

info.plist背景模块

UIBackgroundModes
        fetch
        processing
        remote-notification

I installed firebase notification package in my Flutter project and setup the necessary settings for sending data.

Everything works fine on android, when the app is completely closed, when I send a parameter via firebase, the page I want opens, but on the ios part, when I click on the notification, it does not catch the click and the page I want does not open.

I would be very happy if you could help me where is the main point I should pay attention to for the ios part.

Main.dart

  initialMessage = await FirebaseMessaging.instance.getInitialMessage();
  if (initialMessage != null) {
    if (initialMessage!.data['type'] == 'notification') {
      await Future.delayed(const Duration(seconds: 4));
      navigatorKey.currentState?.pushNamed(
          '/notification'); // navigate to login, with null-aware check

    }
  } 

pushNotificationService.dart

class PushNotificationService {

  Future<void> setupInteractedMessage() async {
    await Firebase.initializeApp();

    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();
 
    if (initialMessage != null &&
        initialMessage.data['type'] == 'notification') {
      await Future.delayed(const Duration(seconds: 6));
      navigatorKey.currentState?.pushNamed(
          '/notification'); // navigate to login, with null-aware check

    }
// Also handle any interaction when the app is in the background via a
    // Stream listener
    // This function is called when the app is in the background and user clicks on the notification
    FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
      // Get.toNamed(NOTIFICATIOINS_ROUTE);
      print(message.toMap().toString());
      print(message.data['type'].toString());
      if (message.data['type'] == 'notification') {
        navigatorKey.currentState?.pushNamed('/notification');
      }
    });
    await enableIOSNotifications();
    await registerNotificationListeners();
  }

  registerNotificationListeners() async {
    AndroidNotificationChannel channel = androidNotificationChannel();
    final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
        FlutterLocalNotificationsPlugin();
    await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.createNotificationChannel(channel);
    var androidSettings =
        const AndroidInitializationSettings('@mipmap/ic_launcher');
    var iOSSettings = const IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,
    );
    var initSetttings =
        InitializationSettings(android: androidSettings, iOS: iOSSettings);
    flutterLocalNotificationsPlugin.initialize(initSetttings,
        onSelectNotification: (message) async {
      // This function handles the click in the notification when the app is in foreground
      // Get.toNamed(NOTIFICATIOINS_ROUTE);
    });
// onMessage is called when the app is in foreground and a notification is received
    FirebaseMessaging.onMessage.listen((RemoteMessage? message) {
      // Get.find<HomeController>().getNotificationsNumber();
      print(message!.data['type'].toString());
      print(message.toMap().toString());
      if (message.data['type'] == 'notification') {
        navigatorKey.currentState?.pushNamed('/notification');
      }
      RemoteNotification? notification = message.notification;
      AndroidNotification? android = message.notification?.android;
// If `onMessage` is triggered with a notification, construct our own
      // local notification to show to users using the created channel.
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
          notification.hashCode,
          notification.title,
          notification.body,
          NotificationDetails(
            android: AndroidNotificationDetails(
              channel.id,
              channel.name,
              icon: android.smallIcon,
              playSound: true,
            ),
          ),
        );
      }
    });
  }

  enableIOSNotifications() async {
    await FirebaseMessaging.instance
        .setForegroundNotificationPresentationOptions(
      alert: true, // Required to display a heads up notification
      badge: true,
      sound: true,
    );
  }

  androidNotificationChannel() => const AndroidNotificationChannel(
        'high_importance_channel', // id
        'High Importance Notifications', // title
        importance: Importance.max,
      );
}

info.plist background modules

UIBackgroundModes
        fetch
        processing
        remote-notification

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文