IChannelsListener

Interface defining callbacks for handling events related to Channels within the SDK.

Implement this interface to receive notifications about channel events such as incoming channel messages, channel message deleted, and other channel-related activities.

Samples

import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.PermissionChecker
import com.ale.R
import com.ale.infra.contact.IRainbowContact
import com.ale.infra.manager.IMMessage
import com.ale.infra.manager.channel.Channel
import com.ale.infra.manager.channel.ChannelItem
import com.ale.infra.proxy.conversation.IRainbowConversation
import com.ale.rainbowsdk.Channels
import com.ale.rainbowsdk.Im
import com.ale.rainbowsdk.Infrastructure
import com.ale.rainbowsdk.RainbowSdk
fun main() { 
   //sampleStart 
   class NotificationsSample(private val applicationContext: Context) : Channels.IChannelsListener, Im.IRainbowImListener {

    private val sampleChannelID = "SAMPLE_CHANNEL_ID"
    private val channelGroupID = "CHANNEL_GROUP_ID"

    private val sampleMessageID = "SAMPLE_MESSAGE_ID"
    private val messageGroup = "MESSAGE_GROUP_ID"


    @RequiresApi(Build.VERSION_CODES.O)
    fun start() {
        val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager?
        notificationManager?.createNotificationChannel(NotificationChannel(sampleChannelID, "Default notification", NotificationManager.IMPORTANCE_DEFAULT))
        RainbowSdk().channels().registerChannelsListener(this)
        RainbowSdk().im().registerListener(this)
    }

    fun stop() {
        RainbowSdk().channels().unregisterChannelsListener(this)
        RainbowSdk().im().unregisterListener(this)
    }

    // Display notifications when a channel message is received
    @SuppressLint("MissingPermission")
    override fun notifyChannelMessage(channelItem: ChannelItem, channel: Channel) {
        val contactName = channelItem.contact?.getDisplayName("Unknown") ?: "Unknown"
        val text = "$contactName has published a new message in the channel"

        val notification = NotificationCompat.Builder(applicationContext, sampleChannelID)
            .setGroup(channelGroupID)
            .setSmallIcon(R.drawable.subscribed)
            .setColor(ContextCompat.getColor(applicationContext, R.color.blue_rainbow))
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setContentTitle(channel.name)
            .setContentText(text)
            .build()

        // Check whether the notifications permissions have been accepted
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || PermissionChecker.checkSelfPermission(applicationContext, Manifest.permission.POST_NOTIFICATIONS) == PermissionChecker.PERMISSION_GRANTED) {
            NotificationManagerCompat.from(applicationContext).notify(channel.id.hashCode(), notification)
        }
    }

    @SuppressLint("MissingPermission")
    override fun onImReceived(conversation: IRainbowConversation, message: IMMessage?) {
        if (message == null) return

        val contact = RainbowSdk().contacts().getContactFromJid(message.contactJid) ?: return

        val notification = NotificationCompat.Builder(applicationContext, sampleMessageID)
            .setGroup(messageGroup)
            .setSmallIcon(R.drawable.subscribed)
            .setColor(ContextCompat.getColor(applicationContext, R.color.blue_rainbow))
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setContentTitle(contact.getDisplayName("Unknown"))
            .setContentText(message.messageContent)
            .build()

        // Check whether the notifications permissions have been accepted
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || PermissionChecker.checkSelfPermission(applicationContext, Manifest.permission.POST_NOTIFICATIONS) == PermissionChecker.PERMISSION_GRANTED) {
            NotificationManagerCompat.from(applicationContext).notify(conversation.id.hashCode(), notification)
        }
    }

    @SuppressLint("MissingPermission")
    override fun notifyNewMessageWithApplicationStopped(conversation: IRainbowConversation, contact: IRainbowContact, message: IMMessage) {
        val notification = NotificationCompat.Builder(applicationContext, sampleMessageID)
            .setGroup(messageGroup)
            .setSmallIcon(R.drawable.subscribed)
            .setColor(ContextCompat.getColor(applicationContext, R.color.blue_rainbow))
            .setAutoCancel(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setContentTitle(contact.getDisplayName("Unknown"))
            .setContentText(message.messageContent)
            .build()

        // Check whether the notifications permissions have been accepted
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || PermissionChecker.checkSelfPermission(applicationContext, Manifest.permission.POST_NOTIFICATIONS) == PermissionChecker.PERMISSION_GRANTED) {
            NotificationManagerCompat.from(applicationContext).notify(conversation.id.hashCode(), notification)
        }
    }
} 
   //sampleEnd
}

Functions

Link copied to clipboard
open fun notifyChannelMarkedAsRead(channel: Channel)

Invoked right after invoking Channels.markAllItemsAsRead from the Channels module.

Link copied to clipboard
open fun notifyChannelMessage(channelItem: ChannelItem, channel: Channel)

Invoked when the user receives a new channel message in one of their channels.

Link copied to clipboard
open fun notifyChannelMessageDeleted(channelId: String, channelItemId: String)

Invoked when a channel message is deleted.

Link copied to clipboard
open fun notifyChannelMessageMarkedAsRead(channelItem: ChannelItem)

Invoked when the isRead property of a ChannelItem is set to true.

Link copied to clipboard
open fun notifyChannelUnsubscribed(channel: Channel)

Invoked when the user unsubscribes from a channel or the channel is deleted.