首页 > 解决方案 > OneSignal 推送通知 | 在聊天时禁用它们进行聊天| 安卓

问题描述

我正在Android 中构建一个一对一的聊天应用程序。一切正常,但我想知道如何避免在对话打开时收到对话通知。

示例:我正在与B通话,B向我发送了一条消息,我在聊天视图中看到了这条消息,但我也收到了通知,告诉我 B 向你发送了一条消息。

我怎样才能避免这种情况?我想不出一个有效的解决方案。

标签: androidkotlinpush-notificationchatonesignal

解决方案


您可以使用此处理程序来决定是否应该显示通知

class NotificationWillShowInForegroundHandler : OneSignal.OSNotificationWillShowInForegroundHandler {

    override fun notificationWillShowInForeground(notificationReceivedEvent: OSNotificationReceivedEvent?) {
        when (isHideInForeground) {
            true -> {
                notificationReceivedEvent?.complete(null)
            }
            else -> {
                notificationReceivedEvent?.complete(notificationReceivedEvent.notification)
            }
        }
    }
}

初始化

OneSignal.setNotificationWillShowInForegroundHandler(NotificationWillShowInForegroundHandler())

推荐阅读