mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 01:45:22 +00:00
notifications
This commit is contained in:
@@ -35,6 +35,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
|
|||||||
private val channelManager = ChannelManager(state, messageManager, dataManager, viewModelScope)
|
private val channelManager = ChannelManager(state, messageManager, dataManager, viewModelScope)
|
||||||
val privateChatManager = PrivateChatManager(state, messageManager, dataManager)
|
val privateChatManager = PrivateChatManager(state, messageManager, dataManager)
|
||||||
private val commandProcessor = CommandProcessor(state, messageManager, channelManager, privateChatManager)
|
private val commandProcessor = CommandProcessor(state, messageManager, channelManager, privateChatManager)
|
||||||
|
private val notificationManager = NotificationManager(application.applicationContext)
|
||||||
|
|
||||||
// Delegate handler for mesh callbacks
|
// Delegate handler for mesh callbacks
|
||||||
private val meshDelegateHandler = MeshDelegateHandler(
|
private val meshDelegateHandler = MeshDelegateHandler(
|
||||||
@@ -42,6 +43,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), B
|
|||||||
messageManager = messageManager,
|
messageManager = messageManager,
|
||||||
channelManager = channelManager,
|
channelManager = channelManager,
|
||||||
privateChatManager = privateChatManager,
|
privateChatManager = privateChatManager,
|
||||||
|
notificationManager = notificationManager,
|
||||||
coroutineScope = viewModelScope,
|
coroutineScope = viewModelScope,
|
||||||
onHapticFeedback = { ChatViewModelUtils.triggerHapticFeedback(context) },
|
onHapticFeedback = { ChatViewModelUtils.triggerHapticFeedback(context) },
|
||||||
getMyPeerID = { meshService.myPeerID }
|
getMyPeerID = { meshService.myPeerID }
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ class MeshDelegateHandler(
|
|||||||
private val messageManager: MessageManager,
|
private val messageManager: MessageManager,
|
||||||
private val channelManager: ChannelManager,
|
private val channelManager: ChannelManager,
|
||||||
private val privateChatManager: PrivateChatManager,
|
private val privateChatManager: PrivateChatManager,
|
||||||
|
private val notificationManager: NotificationManager,
|
||||||
private val coroutineScope: CoroutineScope,
|
private val coroutineScope: CoroutineScope,
|
||||||
private val onHapticFeedback: () -> Unit,
|
private val onHapticFeedback: () -> Unit,
|
||||||
private val getMyPeerID: () -> String
|
private val getMyPeerID: () -> String
|
||||||
@@ -45,6 +46,9 @@ class MeshDelegateHandler(
|
|||||||
if (message.isPrivate) {
|
if (message.isPrivate) {
|
||||||
// Private message
|
// Private message
|
||||||
privateChatManager.handleIncomingPrivateMessage(message)
|
privateChatManager.handleIncomingPrivateMessage(message)
|
||||||
|
if (state.getSelectedPrivateChatPeerValue() != message.senderPeerID) {
|
||||||
|
notificationManager.showPrivateMessageNotification(message.sender, message.content)
|
||||||
|
}
|
||||||
} else if (message.channel != null) {
|
} else if (message.channel != null) {
|
||||||
// Channel message
|
// Channel message
|
||||||
if (state.getJoinedChannelsValue().contains(message.channel)) {
|
if (state.getJoinedChannelsValue().contains(message.channel)) {
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.bitchat.android.ui
|
||||||
|
|
||||||
|
import android.app.NotificationChannel
|
||||||
|
import android.app.NotificationManager
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.core.app.NotificationCompat
|
||||||
|
import com.bitchat.android.R
|
||||||
|
|
||||||
|
class NotificationManager(private val context: Context) {
|
||||||
|
|
||||||
|
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
|
private val channelId = "bitchat_notifications"
|
||||||
|
|
||||||
|
init {
|
||||||
|
createNotificationChannel()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun createNotificationChannel() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
val name = "Bitchat Notifications"
|
||||||
|
val descriptionText = "Notifications for new messages and other events"
|
||||||
|
val importance = NotificationManager.IMPORTANCE_DEFAULT
|
||||||
|
val channel = NotificationChannel(channelId, name, importance).apply {
|
||||||
|
description = descriptionText
|
||||||
|
}
|
||||||
|
notificationManager.createNotificationChannel(channel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun showPrivateMessageNotification(sender: String, message: String) {
|
||||||
|
val builder = NotificationCompat.Builder(context, channelId)
|
||||||
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setContentTitle("New message from $sender")
|
||||||
|
.setContentText(message)
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
|
.setAutoCancel(true)
|
||||||
|
|
||||||
|
notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user