简体   繁体   English

Flutter Firebase 如何控制通知在应用程序处于前台时不显示

[英]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

Update:更新:

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 问题,链接


Original Answer:原答案:

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.如果您想在overlayIn 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.

相关问题 当应用程序为前台时如何显示本地通知 - How to show local notification when app is foreground 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 当应用程序在 iOS 前台时如何处理 Flutter FCM 推送通知? - How to handle Flutter FCM push notification when app in foreground for iOS? 对于本地通知,当应用程序在前台运行时,如何显示“警报”? - For Local Notification, how to show a “Alert” when the app is running in the foreground? Ionic 2 - 当应用程序在IOS上处于前台时显示通知 - Ionic 2 - Show notification when app is in foreground on IOS 如果应用程序在前台,如何停止 firebase 通知 - How to stop firebase notification if app is in foreground 当应用程序处于前台时如何显示Firebase通知 - How to show firebase notifications when application is in foreground 应用程序处于前台时未收到推送通知 ios flutter - Push Notification not received when app is in foreground ios flutter Flutter iOS 应用程序在前台时的 FCM 推送通知 - Flutter FCM push notification for iOS when app is in foreground iOS 10,本地通知会在应用程序处于前台时显示? - iOS 10, Local notification show when app is in foreground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM