可以使用NotFification Tap导航到特定页面。飘扬的深链接问题

发布于 01-22 23:24 字数 1849 浏览 4 评论 0原文

//root dart file

  FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler); 
  await NotificationService.instance.initializeNotifications();

  Future<void> _backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
 }

//In, NotificationService file, I have initialized 
 AwesomeNotification(awesome_notification package),and have 

  //This handles notification actions

  AwesomeNotifications().actionStream.listen((notification) async {
  print(notification.payload);
  if (notification.payload != null) {
    final payload = notification.payload!;
    if (payload['type'] == 'vaccine-records') {
      _appRouter.root.innerRouterOf(DashboardRouter.name)
        ?..innerRouterOf<TabsRouter>(DashboardRoute.name)?.setActiveIndex(2)
        ..navigate(
          VaccineRecordsRouter(
            petId: int.parse(payload['id']!),
            petType: int.parse(payload['pet_type']!),
            petName: notification.title,
          ),
        );
    }
  }
});

 //this listens to new notification from Firebase cloud messaging

FirebaseMessaging.onMessage.listen((message) async {
  print(message.data);
  if (message.data.isNotEmpty) {
    await AwesomeNotifications().createNotificationFromJsonData(message.data);
  } else {
    print('here');
    await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 0,
        channelKey: 'basic_channel',
        title: message.notification?.title,
        body: message.notification?.body,
        showWhen: true,
        displayOnForeground: true,
        displayOnBackground: true,
      ),
    );
  }
});
}

当我点击通知时,它将带我进入应用程序的主页。我想要它 将我导航到其他屏幕。当该应用在前景中时,我收到 通知,这将我带到所需的页面。但是当应用在后台 并收到了通知,这需要我进入主页。这是如何发生的 从两次开始,我都会得到awesomenotification()。ActionStream.Listen((通知) 异步{}执行?

//root dart file

  FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler); 
  await NotificationService.instance.initializeNotifications();

  Future<void> _backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
 }

//In, NotificationService file, I have initialized 
 AwesomeNotification(awesome_notification package),and have 

  //This handles notification actions

  AwesomeNotifications().actionStream.listen((notification) async {
  print(notification.payload);
  if (notification.payload != null) {
    final payload = notification.payload!;
    if (payload['type'] == 'vaccine-records') {
      _appRouter.root.innerRouterOf(DashboardRouter.name)
        ?..innerRouterOf<TabsRouter>(DashboardRoute.name)?.setActiveIndex(2)
        ..navigate(
          VaccineRecordsRouter(
            petId: int.parse(payload['id']!),
            petType: int.parse(payload['pet_type']!),
            petName: notification.title,
          ),
        );
    }
  }
});

 //this listens to new notification from Firebase cloud messaging

FirebaseMessaging.onMessage.listen((message) async {
  print(message.data);
  if (message.data.isNotEmpty) {
    await AwesomeNotifications().createNotificationFromJsonData(message.data);
  } else {
    print('here');
    await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 0,
        channelKey: 'basic_channel',
        title: message.notification?.title,
        body: message.notification?.body,
        showWhen: true,
        displayOnForeground: true,
        displayOnBackground: true,
      ),
    );
  }
});
}

When I tap on the notification, it takes me to homepage of my app. I want it to
navigate me to some other screen.When the app is in the foreground and I receive the
notification, it takes me to the desired page. But when the app is in the background
and the notification is received, it takes me to the homepage.How is this happening
since the both time i get AwesomeNotifications().actionStream.listen((notification)
async {} to execute?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷2025-01-29 23:24:31

我建议您根据通知的有效载荷使用Firebase动态链接将用户发送到特定页面。

在您当前的情况下,

仅当应用在前景时才触发onMessage函数。

必须处理3个州的通知。

  • 当应用在
  • 应用时,当应用在App在后台
  • 终止

时,请使用以下内容:

//when app is terminated
  FirebaseMessaging.instance.getInitialMessage().then((value) {
    if (value != null) {
      _onNotificationMessage(context, value); // custom function to handle notification
    }
  });

//when app is in foreground
  FirebaseMessaging.onMessage.listen((message) {
    _onProcessMessage(context, message); // custom function to handle notification
  });

//when app is in background
  FirebaseMessaging.onMessageOpenedApp.listen((message) {
    _onNotificationMessage(context, message); // custom function to handle notification
  });
}

I would suggest you use Firebase dynamic links to send the users to specific page based on the payload of notification.

In your current case,

onMessage function is triggered only when the app is in foreground.

Notifications must be handled for 3 states.

  • When app is in foreground
  • When app is in background
  • When app is terminated

Use the following :

//when app is terminated
  FirebaseMessaging.instance.getInitialMessage().then((value) {
    if (value != null) {
      _onNotificationMessage(context, value); // custom function to handle notification
    }
  });

//when app is in foreground
  FirebaseMessaging.onMessage.listen((message) {
    _onProcessMessage(context, message); // custom function to handle notification
  });

//when app is in background
  FirebaseMessaging.onMessageOpenedApp.listen((message) {
    _onNotificationMessage(context, message); // custom function to handle notification
  });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文