【问题标题】:Expo: don't show notification if app is open in foregroundExpo:如果应用程序在前台打开,则不显示通知
【发布时间】:2019-06-20 15:50:24
【问题描述】:

我正在使用 Expo 开发一个 react-native 消息传递应用程序。每次用户收到新消息时,我都会从服务器发送通知。

如果应用当前处于打开状态,有什么方法可以不显示通知?

现在我一收到通知就使用这个:

Notifications.dismissNotificationAsync(notification.notificationId);

但是有 0.5 秒的延迟,通知有时间出现在托盘中并在它被解除之前触发声音。我不想显示它。

【问题讨论】:

    标签: android react-native expo


    【解决方案1】:

    您的问题的答案是

    如果应用程序是,有什么办法不显示通知 目前营业吗?

    Notification in Expo 的默认行为是在应用程序处于前台时不显示通知。你一定已经实现了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,
      }),
    });
    */
    

    如果您不使用setNotificaitonHandler,则应用在前台时不会显示新通知。

    【讨论】:

      【解决方案2】:

      在应用运行时收到通知时,您可以使用setNotificationHandler 设置回调来决定是否应向用户显示通知。

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

      收到通知时,将调用handleNotification,并将传入通知作为参数。该函数应在 3 秒内响应一个行为对象,否则该通知将被丢弃。如果通知处理成功,则使用通知的标识符调用handleSuccess,否则(或超时)将调用handleError

      未设置处理程序或未及时响应时的默认行为是不显示通知。

      如果不使用 setNotificaitonHandler,则应用在前台时不会显示新的通知。

      因此,您可以在应用初始化时简单地将setNotificationHandler 设置为null

      
      Notifications.setNotificationHandler(null);
      
      

      See Documentaition

      【讨论】:

      • 有没有办法根据通知的类型自定义setNotificationHandler?即在特定页面上聊天时,我希望能够接收通知并弹出通知我有关新用户加入的信息。但是,我不想收到一个弹出窗口告诉我有人刚刚在聊天页面上发布了一条消息,因为我可以看到它。谢谢!
      【解决方案3】:

      使用下面的代码 sn-p。它适用于新闻通知。

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

      【讨论】:

        【解决方案4】:

        我假设您设置了一个简单的 FCM - Firebase cloud messaging 并使用它向客户端推送消息? The official Expo guide has a section for receiving-push-notifications

        【讨论】:

        • 我不确定此代码将如何阻止通知出现。除非我遗漏了什么,否则它所做的只是在托盘中接收所述通知的顶部的视图中显示通知的内容。
        • 另一种选择是根本不发送通知并使用套接字传输所需的数据。
        • 不知道自编写此答案以来该文档是否已更新。但至少在今天,该链接描述了 setNotificationHandler 应该如何在前台隐藏通知。
        【解决方案5】:

        这是 FCM 的实际工作流程(奇怪可以称为常见问题),它会在应用程序处于前台时自行处理通知。

        我为我的项目所做的解决方案是创建一个自定义通知 JSON,而不是使用 FCM 不会解析的默认模板。

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

        在控制台中,您可以添加自己的自定义 JSON 对象,当您收到通知时,使用这些对象解析通知,然后您将能够覆盖该问题。

        您还可以为请求添加频道以对通知进行分类

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

        【讨论】:

        • 您能详细说明一下吗?我不确定你在哪里使用这个 JSON 对象。
        • 我可以通过 Expo 做些什么吗?因为我正在使用 ExpoPush 通知。
        • @Ryan Pergent 是的,因为它并不难,只需从控制台发送自定义通知并获取通知,就像我在这里用你的密钥编写的代码一样,肯定会工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-10
        • 1970-01-01
        • 2019-12-30
        • 2015-08-31
        相关资源
        最近更新 更多