[英]flutter_local_notifications onNotificationReceived method - Flutter
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()
动作。
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()
函数触发。In my project I am using:在我的项目中,我正在使用:
flutter_local_notifications
package. flutter_local_notifications
包。 When an event is scheduled in my app, the process is the following:在我的应用程序中安排事件时,过程如下:
POST
request to FCM with data only message.POST
请求。onMessageReceived()
callback.onMessageReceived()
回调接收消息。'Got a message whilst in the foreground!'
'Got a message whilst in the foreground!'
message.flutter_local_notifications
to schedule a notification.flutter_local_notifications
来安排通知。 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 上安排通知,因为你不能从发布请求(奇怪)中做到这一点,但这会解决我所有的问题。
Overlay
with the notification, in case of the user being in the foregroundOverlay
notificationCount
in my Firebase Realtime DatabasenotificationCount
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()` 动作。
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.根据您的代码,
scheduledDate
和formattedDate
知道它。 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.