[英]Flutter Firebase how to control notification not to show when the app is in foreground
I had a flutter app with a chat room feature.我有一个带有聊天室功能的 flutter 应用程序。 Firebase messaging is used to push remote notification.
Firebase 消息用于推送远程通知。 I want to hide the notification popup if the app is at foreground and it happens to be at the exact chat room where that coming notification belongs to.
如果应用程序位于前台并且它恰好位于即将到来的通知所属的确切聊天室,我想隐藏通知弹出窗口。
This is a common behavior when you're chatting with someone.这是您与某人聊天时的常见行为。 The message appends to chat room directly instead of popup.
消息直接附加到聊天室而不是弹出窗口。
My question is how to interfere the remote notification on receiving?我的问题是如何在接收时干扰远程通知? I used onMessage callback on notification received event.
我在收到通知事件时使用了onMessage回调。 But it always popups no matter the app is at foreground or background or termiated.
但无论应用程序处于前台还是后台或终止,它总是会弹出。
In firebase React Native version, it has an onNotification callback which does exactly what I want.在 firebase React Native 版本中,它有一个onNotification回调,这正是我想要的。 But there is no equivalence for flutter.
但是 flutter 没有等价物。 My research leads me to send Data and Notification type of message.
我的研究导致我发送数据和通知类型的消息。 In such a way, I need to keep app status synchronized with my backend server (Am I right?).
这样,我需要保持应用程序状态与我的后端服务器同步(对吗?)。
Any help or suggestion is welcome.欢迎任何帮助或建议。 Thank you.
谢谢你。
PS firebase_messaging 8.0.0-dev.10 PS firebase_messaging 8.0.0-dev.10
As mentioned by op, the problem was fixed when using to the latest stable version (7.0.3).正如操作所提到的,使用最新的稳定版本(7.0.3)时问题已得到解决。 Then the cause is a bug in the new dev release.
然后原因是新开发版本中的错误。
Related GitHub issue, Link相关 GitHub 问题,链接
Judging by the issues that you described, the problem that you are running into might be because you are on the dev channel.从您描述的问题来看,您遇到的问题可能是因为您在开发频道上。
Did you try to use the latest stable?您是否尝试使用最新的稳定版? (7.0.3 at the time of writing).
(撰写本文时为 7.0.3)。 It might be a bug in the dev channel.
这可能是开发频道中的错误。
If the problem persists, try sending a data message instead of the notification message.如果问题仍然存在,请尝试发送数据消息而不是通知消息。 This will stop firebase from sending a notification on the device.
这将阻止 firebase 在设备上发送通知。 But be aware that you will have to display a notification manually.
但请注意,您必须手动显示通知。 This way you will have control over the display of the notifications based on the screen and wouldn't have to keep the state in sync with the server as you were mentioning.
这样,您将可以控制基于屏幕的通知显示,并且不必像您提到的那样使 state 与服务器保持同步。
But this might not work on iOS since according to the docs , onMessage()
will be triggered when the app becomes in foreground.但这可能不适用于 iOS,因为根据文档,当应用程序进入前台时将触发
onMessage()
。
Check the link above for more details on how the notification message and the data message gets handled on both Android and iOS.查看上面的链接,了解有关如何在 Android 和 iOS 上处理通知消息和数据消息的更多详细信息。
If your onMessage does not have code to handle the notifications, then they shouldn't appear.如果您的 onMessage 没有处理通知的代码,那么它们不应该出现。
If You want to convert your onMessage
notification, in a overlay
or In App Notification
then it would be better.如果您想在
overlay
或In App Notification
中转换您的onMessage
通知,那就更好了。
I was also facing the same Issue for showing the notification
, and found a workaround for that, We can show the In App Notifications
in our App我在显示
notification
时也面临同样的问题,并找到了解决方法,我们可以在我们的应用程序中显示In App Notifications
Instead of showing the notification code in your onMessage
method, you can show a overlay
, like instagram shows on the top when a notification
arrives,除了在
onMessage
方法中显示通知代码之外,您还可以显示一个overlay
,例如当notification
到达时 instagram 在顶部显示,
And to append the message to direct chat room, I used some backend help.并将 append 的消息发送到直接聊天室,我使用了一些后端帮助。
onMessage: (Map<String, dynamic> message) async {
showOverlayNote(message);
},
And now you can show the overlay
as现在您可以将
overlay
显示为
showOverlayNote(message){
//your code here for showing the overlay
}
You Can get a Detail on how to use overlay for In App Notificaions from this link您可以从此链接获取有关如何在应用内通知中使用叠加层的详细信息
This guy have explained the use of this In App Notifications better, So Please see the above link.这家伙已经更好地解释了这个 In App Notifications 的使用,所以请看上面的链接。
You're thinking about it from the wrong angle.你从错误的角度考虑它。 Firebase messaging is always going to trigger your
onMessage
, it's up to you how you handle that internally. Firebase 消息传递总是会触发你的
onMessage
,这取决于你如何在内部处理它。
I'd suggest using a state manager - Mobx, ChangeNotifier, a file, whatever - to keep track of what screen the user is on.我建议使用 state 管理器——Mobx、ChangeNotifier、文件等等——来跟踪用户在哪个屏幕上。 When the user enters your chat screen, write that to your state manager (also "un-write" that upon your
dispose()
method if necessary).当用户进入您的聊天屏幕时,将其写入您的 state 管理器(如有必要,也可以在您的
dispose()
方法中“取消写入”)。
Then, in the method that handles onMessage
, check for the user's current screen.然后,在处理
onMessage
的方法中,检查用户的当前屏幕。 If they're on the chat screen then exit, if not, execute the overlay.如果他们在聊天屏幕上,则退出,如果不是,则执行覆盖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.