GetStream / stream-chat-android

:speech_balloon: Android Chat SDK ➜ Stream Chat API. UI component libraries for chat apps. Kotlin & Jetpack Compose messaging SDK for Android chat
https://getstream.io/chat/sdk/android/
Other
1.44k stars 269 forks source link

Feature request: Ability to disable the `isForeground` check for push notifications in order to show push notifications even if the app is in the foreground #1211

Closed ezet closed 3 years ago

ezet commented 3 years ago

Chat is only a minor part of our application, and new chat messages might not be immediately visible even if the app is running in the foreground. We therefore want to display chat messages even if the app has focus, or have more fine-grained control of when to handle push messages and not.

This means making changes in the 3 locations.

https://github.com/GetStream/stream-chat-android/blob/master/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/notifications/ChatNotifications.kt#L65 https://github.com/GetStream/stream-chat-android/blob/master/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/notifications/ChatNotifications.kt#L75

In the above two locations, the isForeground() check could be moved inside the handler.onChatEvent() and handler.onFirebaseMessage() methods, which already provides an an open interface. The default implementation could be return isForeground() which would abort the push notification when the app is in the foreground.

The last check here https://github.com/GetStream/stream-chat-android/blob/master/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/notifications/ChatNotifications.kt#L155 doesn't have an equally simple solution, but I guess it could also delegate the check to a possibly new method in the ChatNotificationHandler implementation.

ezet commented 3 years ago

Another approach could be to provide an API that lets us easily disable and enable notification handling as required. That way we could call this API based on the lifecycle state of certain activities. The use-case would be to disable push handling while certain activities are in the foreground, while enable them for others.

samiuelson commented 3 years ago

Hey @ezet, modifying the SDK to display notifications in the foreground is something that would be hard to achieve right now. This is also not guaranteed that we will receive notification event when the web socket connection is active (while the app is in foreground). However, you can just simply subscribe to receive a "new message" events when the app is in foreground and display your notification manually:

ChatClient.subscribeFor(NewMessageEvent::class) {
    // display notification here
}
samiuelson commented 3 years ago

Closing the issue now. @ezet, please let us know if you need any further help on our side.