简体   繁体   English

Expo:如果应用程序在前台打开,则不显示通知

[英]Expo: don't show notification if app is open in foreground

I am developing a react-native messaging app with Expo.我正在开发一个带有 Expo 的 react-native 消息传递应用程序。 Every time a user receives a new message, I send a notification from my server.每次用户收到新消息时,我都会从我的服务器发送通知。

Is there any way to not display the notification if the app is currently open?如果应用程序当前处于打开状态,有没有办法不显示通知?

Right now I am using this as soon as the notification is received:现在我在收到通知后立即使用它:

Notifications.dismissNotificationAsync(notification.notificationId);

But there is a 0.5 second delay where the notification has time to appear in the tray and trigger a sound before it gets dismissed.但是有 0.5 秒的延迟,通知有时间出现在托盘中并在它被关闭之前触发声音。 I would like to not show it at all.我想根本不显示它。

The answer is yes to your question你的问题的答案是肯定

Is there any way to not display the notification if the app is currently open?如果应用程序当前处于打开状态,有没有办法不显示通知?

The default behavior of Notification in Expo is not to show notification if the App is in foreground. ExpoNotification的默认行为是如果应用程序在前台则不显示通知。 You must have implemented Notifications.setNotificationHandler similar to the following code -您必须已经实现了类似于以下代码的Notifications.setNotificationHandler -

// *** DON'T USE THE FOLLOWING CODE IF YOU DON'T WANT NOTIFICATION TO BE DISPLAYED
// WHILE THE APP IS IN FOREGROUND! ***
// --------------------------------------------------
// Sets the handler function responsible for deciding
// what to do with a notification that is received when the app is in foreground
/*
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});
*/

If you don't use setNotificaitonHandler , the new notifications will not be displayed while the app is in foreground.如果您不使用setNotificaitonHandler ,则当应用程序处于前台时将不会显示新通知。

When a notification is received while the app is running , using setNotificationHandler you can set a callback that will decide whether the notification should be shown to the user or not.应用程序运行时收到通知,使用setNotificationHandler可以设置一个回调,该回调将决定是否应向用户显示通知。

Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: false,
    shouldSetBadge: false,
  }),
});

When a notification is received, handleNotification is called with the incoming notification as an argument.当收到通知时,将调用handleNotification并将传入的通知作为参数。 The function should respond with a behavior object within 3 seconds, otherwise the notification will be discarded.该函数应在 3 秒内响应一个行为对象,否则将丢弃通知。 If the notification is handled successfully, handleSuccess is called with the identifier of the notification, otherwise (or on timeout) handleError will be called.如果通知处理成功,将使用通知的标识符调用handleSuccess ,否则(或超时)将调用handleError

The default behavior when the handler is not set or does not respond in time is not to show the notification.未设置处理程序或未及时响应时的默认行为是不显示通知。

If you don't use setNotificaitonHandler, the new notifications will not be displayed while the app is in foreground.如果您不使用 setNotificaitonHandler,则当应用程序处于前台时,将不会显示新通知。

So you can simply set setNotificationHandler to null when your app is initialized.因此,您可以在应用程序初始化时简单地将setNotificationHandler设置为null


Notifications.setNotificationHandler(null);

See Documentaition 查看文档

Use below code snippet.使用下面的代码片段。 It works on press notification.它适用于新闻通知。

_handleNotification = async (notification) => {
      const {origin} = notification;
      if (origin === ‘selected’) {
            this.setState({notification: notification});
      }
//OR
      if (AppState.currentState !== 'active') {
            this.setState({notification: notification});
      }

 } 

I assume you setup a simple FCM - Firebase cloud messaging And use that to push messages to the client?我假设您设置了一个简单的 FCM - Firebase 云消息传递并使用它向客户端推送消息? The official Expo guide has a section for receiving-push-notifications 官方世博指南有一个用于接收推送通知的部分

This is the actual workflow of FCM (weird can be called as a common issue) that it'll handle the notifications by itself when the application is in the foreground.这是 FCM 的实际工作流程(很奇怪可以称为常见问题),当应用程序处于前台时,它会自行处理通知。

The solution which i did for my project was to create a custom notification JSON rather than using their default template which won't be parsed by FCM.我为我的项目所做的解决方案是创建一个自定义通知 JSON,而不是使用 FCM 不会解析的默认模板。

{
"hello":" custom key and value",
"message":{
"SampleKey":"Sample data",
"data":{
"SampleKey" : "Sampledata",
"SampleKey2" : "great match!"},
 }}

In console you can add your own custom JSON objects, and when you get the notification parse the notification by using these objects, then you will be able to override that issue.在控制台中,您可以添加自己的自定义 JSON 对象,当您收到通知时,使用这些对象解析通知,然后您将能够覆盖该问题。

You can also add a channel for the request to categorize your notifications您还可以为请求添加频道以对通知进行分类

this.createNotificationListeners  = firebase.notifications()
.onNotification((notification) => {
let{ hello,data,message} = notification;
});

看这里

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

相关问题 Android通知:打开应用程序时不显示通知吗? - Notification Android : Don't show notification when app is open? 不要在Android中通知时触发前台中的应用程序 - Don't fire the app in the foreground on tap on notification in android 使用应用程序时(在前台运行)请勿调用推送通知 - Don't invoke push notification when app is being used (running in foreground) 当应用程序处于前台状态时如何在通知栏中显示通知 - how to show notification to notification tray when app is in foreground 应用处于打开状态或后台(无论是否显示通知) - App open or background (to show notification or not) 如果在后台Android Xamarin中,获取通知消息以在前台显示我的应用程序 - Getting a notification message to show my app in foreground if in background Android Xamarin Android Workmanager,即使应用程序已经在前台,也会加快工作显示通知 - Android Workmanager, expedited work show notification even if app is already in foreground Android 8. +上不会显示前台服务的通知 - A notification of the foreground service doesn't show on Android 8.+ 前台服务通知无法显示,太多东西 - Foreground service notification can't show up, too many things 奥利奥 - 前景服务不显示前景通知 - Oreo - Foreground service does not show foreground notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM