简体   繁体   English

flutter_local_notifications onNotificationReceived 方法 - Flutter

[英]flutter_local_notifications onNotificationReceived method - Flutter

Too long to read: The problem阅读时间过长:问题

Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user.基本上,我正在寻找一个onMessageReceived()回调,但它适用于flutter_local_notifications,因此我可以处理数据并将其显示给用户。 This plugin only supports handling the onNotificationTap() action.这个插件只支持处理onNotificationTap()动作。

  • How am I expected to handle the message when the user receives it, for example, if they have Do Not Disturb on?当用户收到消息时,例如,如果他们启用了Do Not Disturb ”,我应该如何处理该消息? Even if the local notification doesn't show, I need to show an Overlay at least, triggered by some onMessageReceived() function.即使本地通知没有显示,我至少需要显示一个覆盖,由一些onMessageReceived()函数触发。
  • How can I update the notificationCount in my database when a local notification is received (scheduled)?收到(预定)本地通知时,如何更新数据库中的 notificationCount?

Description描述

In my project I am using:在我的项目中,我正在使用:

  1. Firebase Cloud Messaging (FCM) Firebase 云消息传递 (FCM)
  2. flutter_local_notifications package. flutter_local_notifications包。

When an event is scheduled in my app, the process is the following:在我的应用程序中安排事件时,过程如下:

  1. POST request to FCM with data only message.使用仅数据消息向 FCM 发出POST请求。
  2. My app receives the message through the onMessageReceived() callback.我的应用通过onMessageReceived()回调接收消息。
  3. Almost instantly I get 'Got a message whilst in the foreground!'几乎立即我得到'Got a message whilst in the foreground!' message.信息。 This was triggered by the instant FCM data message.这是由即时 FCM 数据消息触发的。
  4. The data inside the message triggers flutter_local_notifications to schedule a notification.消息中的数据会触发flutter_local_notifications来安排通知。
  5. This scheduled local notification, received at a later date, cannot be handled (no OnMessage() function).此计划的本地通知在以后收到,无法处理(没有 OnMessage() 函数)。

I don't schedule a notification directly on FCM because you can't do that from a post request (weird) , but that would solve all my problems.我没有直接在 FCM 上安排通知,因为你不能从发布请求(奇怪)中做到这一点,但这会解决我所有的问题。

Problem问题

  • When the notification gets to the user's device, there is no way of handling the message (foreground or background)当通知到达用户的设备时,无法处理消息(前台或后台)
  • I cannot display an Overlay with the notification, in case of the user being in the foreground如果用户在前台,我无法在通知中显示Overlay
  • I cannot automatically update the notificationCount in my Firebase Realtime Database我无法自动更新 Firebase 实时数据库中的notificationCount

Basically, I am looking for an onMessageReceived() callback but that works with flutter_local_notifications so I can handle and show the data to the user.基本上,我正在寻找一个onMessageReceived()回调,但它适用于flutter_local_notifications,因此我可以处理数据并将其显示给用户。 This plugin only supports handling the onNotificationTap()` action.此插件仅支持处理 onNotificationTap()` 动作。

Example of my process我的过程示例

This is what FCM has that flutter_local_notifications doesn't.这就是 FCM 所具有的而flutter_local_notifications没有的。 Triggered when a notification is received by my app:当我的应用收到通知时触发:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
print('Got a message whilst in the foreground!');

[...]

if (scheduledDate != null) {
//using flutter_local_notifications
  sendScheduledLocalNotification(itemTitle, 'Due Today', formattedDate);
//this notification, received at a later date, cannot be processed with this same function because it doesn't use FCM
}

//only shown with instant notifications (not scheduled)
if (notification != null) {
  showOverlayNotification((context) {
    return LocalNotificationOverlay(
      title: notification.title!,
      subtitle: notification.body!,
      imageUrl: notification.imageUrl!,
    );
  }, duration: Duration(seconds: 3));
}
  }

});

Yes, as far as I know we don't have the same stream option to listen to the notifications in the system tray for flutter_local_notifications as we have for FCM.是的,据我所知,我们没有与 FCM 相同的流选项来收听系统托盘中的flutter_local_notifications通知。

You can check this answer , It might help you.你可以检查这个答案,它可能对你有帮助。

I guess in the end you'd better do it in the back-end, and again this scheduled message being send/received via FCM.我想最后你最好在后端进行,并且再次通过 FCM 发送/接收这个预定的消息。

I don't know about the mechanism that you're using and the function that sends the notification to user, but you can ignore sending notification to user if it has information about scheduling.我不知道您正在使用的机制以及向用户发送通知的功能,但如果它有关于调度的信息,您可以忽略向用户发送通知。 Based on your code, scheduledDate and formattedDate are aware of it.根据您的代码, scheduledDateformattedDate知道它。 So in your cloud function that sends the notifications you can instead of sending a notification, doing some task or triggering something(similar to the attached answer) and this will schedule sending that notification based on its time.因此,在您发送通知的云功能中,您可以代替发送通知、执行某些任务或触发某些事情(类似于附加的答案),这将根据时间安排发送该通知。 Then in front-end for example you can update your databases every time you get a notification from FCM, because you didn't send them in the first place and you actually scheduled sending them in the planed time.然后在前端,例如,您可以在每次收到 FCM 通知时更新您的数据库,因为您一开始并没有发送它们,而是您实际上安排在计划的时间发送它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 计划通知未显示(Flutter_local_notifications) - scheduled notifications not showing (Flutter_local_notifications) 使用 flutter_local_notifications 安排通知 - Schedule notification using flutter_local_notifications 在 flutter_local_notifications 上禁用振动 - Disable vibration on flutter_local_notifications 在 Xcode 中找不到模块“flutter_local_notifications” - Module 'flutter_local_notifications' not found in Xcode 不能使用 flutter_local_notifications - Can't use flutter_local_notifications flutter_local_notifications 对我不起作用 - flutter_local_notifications not working for me flutter_local_notifications 路由全屏 - flutter_local_notifications route fullscreen Flutter:应用程序终止时,flutter_local_notifications onSelectNotification 不起作用 - Flutter : flutter_local_notifications onSelectNotification not working when app is terninated Flutter:使用 FCM 在前台运行时,flutter_local_notifications 不起作用 - Flutter: flutter_local_notifications not work when running in Foreground with FCM 在 Android 的页面开始时立即调用 flutter_local_notifications 的 onSelectNotification 方法(在 iOS 中没有此类问题) - onSelectNotification method of flutter_local_notifications called immediately on page start in Android (in iOS no such issue)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM