Ejemplo n.º 1
0
        private void InitSendBird(Bundle extras)
        {
            mSendBirdInfo = extras;

            String appId    = extras.GetString("appId");
            String uuid     = extras.GetString("uuid");
            String userName = extras.GetString("userName");

            mSyncContext = SynchronizationContext.Current;             // required for api queries

            #region SendBirdEventHandler

            SendBirdEventHandler seh = new SendBirdEventHandler();
            seh.OnConnect         += (sender, e) => {
            };
            seh.OnError           += (sender, e) => {
            };
            seh.OnChannelLeft     += (sender, e) => {
            };
            seh.OnMessageReceived += (sender, e) => {
                SendBirdSDK.MarkAsRead();
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.AddMessageModel(e.Message);
                }, null);
            };
            seh.OnBroadcastMessageReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.AddMessageModel(e.Message);
                }, null);
            };
            seh.OnSystemMessageReceived += (sender, e) => {
                var systemMessage = (SystemMessage)e.Message;
                mSyncContext.Post(delegate {
                    switch (systemMessage.category)
                    {
                    case SystemMessage.CATEGORY_TOO_MANY_MESSAGES:
                        systemMessage.SetMessage("Too many messages. Please try later.");
                        break;

                    case SystemMessage.CATEGORY_MESSAGING_USER_BLOCKED:
                        systemMessage.SetMessage("Blocked.");
                        break;

                    case SystemMessage.CATEGORY_MESSAGING_USER_DEACTIVATED:
                        systemMessage.SetMessage("Deactivated.");
                        break;
                    }

                    mSendBirdMessagingAdapter.AddMessageModel(systemMessage);
                }, null);
            };
            seh.OnFileReceived += (sender, e) => {
            };
            seh.OnReadReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetReadStatus(e.ReadStatus.GetUserId(), e.ReadStatus.timestamp);
                }, null);
            };
            seh.OnTypeStartReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetTypeStatus(e.TypeStatus.GetUserId(), SendBirdUtils.GetCurrentTimeMills());
                }, null);
            };
            seh.OnTypeEndReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.SetTypeStatus(e.TypeStatus.GetUserId(), 0);
                }, null);
            };
            seh.OnAllDataReceived += (sender, e) => {
                mSyncContext.Post(delegate {
                    mSendBirdMessagingAdapter.NotifyDataSetChanged();
                    mSendBirdMessagingFragment.mListView.SetSelection(mSendBirdMessagingAdapter.Count - 1);
                }, null);
            };
            seh.OnMessageDelivery += (sender, e) => {
                if (!e.Sent)
                {
                    mSyncContext.Post(delegate {
                        mSendBirdMessagingFragment.mEtxtMessage.Text = e.Message;
                    }, null);
                }
            };
            seh.OnMessagingStarted += (sender, e) => {
                mSendBirdMessagingAdapter.Clear();
                mSyncContext.Post(delegate {
                    UpdateMessagingChannel(e.MessagingChannel);

                    var channelUrl = e.MessagingChannel.GetUrl();

                    MessageListQuery messageListQuery = SendBirdSDK.QueryMessageList(channelUrl);
                    messageListQuery.OnResult        += (sender_child, e_child) => {
                        // mSyncContext.Post for RunOnUIThread
                        mSyncContext.Post(delegate {
                            foreach (var messageModel in e_child.Messages)
                            {
                                mSendBirdMessagingAdapter.AddMessageModel(messageModel);
                            }

                            mSendBirdMessagingAdapter.NotifyDataSetChanged();
                            mSendBirdMessagingFragment.mListView.SetSelection(30);

                            SendBirdSDK.MarkAsRead(channelUrl);
                            SendBirdSDK.Join(channelUrl);
                            SendBirdSDK.Connect(mSendBirdMessagingAdapter.GetMaxMessageTimestamp());
                        }, null);
                    };
                    messageListQuery.Prev(long.MaxValue, 50);                      // Excute Query
                }, null);
            };
            seh.OnMessagingUpdated += (sender, e) => {
                mSyncContext.Post(delegate {
                    UpdateMessagingChannel(e.MessagingChannel);
                }, null);
            };
            seh.OnMessagingChannelUpdated += (sender, e) => {
                if (mMessagingChannel != null && mMessagingChannel.GetId() == e.MessagingChannel.GetId())
                {
                    mSyncContext.Post(delegate {
                        UpdateMessagingChannel(e.MessagingChannel);
                    }, null);
                }
            };
            seh.OnMentionUpdated += (sender, e) => {
            };

            #endregion

            SendBirdSDK.Init(appId);
            SendBirdSDK.Login(uuid, userName);
            SendBirdSDK.SetEventHandler(seh);
        }
Ejemplo n.º 2
0
    public void InitMessaging()
    {
        currentUserName = inputUserName.text;
        SendBirdSDK.Login(userId, currentUserName);

        SendBirdEventHandler seh = new SendBirdEventHandler();

        seh.OnConnect += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                selectedChannelUrl = e.Channel.url;
            }));
        };
        seh.OnError           += (sender, e) => {
        };
        seh.OnChannelLeft     += (sender, e) => {
        };
        seh.OnMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                Message message          = e.Message as Message;
                txtMessagingContent.text = txtMessagingContent.text + (MessageRichText(message) + "\n");
                ScrollToBottom();
            }));
            // markasread
        };
        seh.OnBroadcastMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                BroadcastMessage bm      = e.Message as BroadcastMessage;
                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(bm.message) + "\n");
            }));
        };
        seh.OnSystemMessageReceived += (sender, e) => {
            mEventProcessor.QueueEvent(new Action(() => {
                SystemMessage sm         = e.Message as SystemMessage;
                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(sm.message) + "\n");
            }));
        };
        seh.OnAllDataReceived   += (sender, e) => {
        };
        seh.OnMessageDelivery   += (sender, e) => {
        };
        seh.OnReadReceived      += (sender, e) => {
        };
        seh.OnTypeStartReceived += (sender, e) => {
        };
        seh.OnTypeEndReceived   += (sender, e) => {
        };
        seh.OnMessagingStarted  += (sender, e) => {
            UpdateMessagingChannel(e.MessagingChannel);

            var messagingChannelUrl = e.MessagingChannel.GetUrl();
            // message query
            MessageListQuery messageListQuery = SendBirdSDK.QueryMessageList(messagingChannelUrl);
            messageListQuery.OnResult += (sender_child, e_child) => {
                mEventProcessor.QueueEvent(new Action(() => {
                    foreach (var messageModel in e_child.Messages)
                    {
                        if (messageModel is Message)
                        {
                            var message = messageModel as Message;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (MessageRichText(message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (MessageRichText(message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                        else if (messageModel is SystemMessage)
                        {
                            var message = messageModel as SystemMessage;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (SystemMessageRichText(message.message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(message.message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                        else if (messageModel is BroadcastMessage)
                        {
                            var message = messageModel as BroadcastMessage;
                            if (message.IsPast())
                            {
                                txtMessagingContent.text = (SystemMessageRichText(message.message) + "\n") + txtMessagingContent.text;
                            }
                            else
                            {
                                txtMessagingContent.text = txtMessagingContent.text + (SystemMessageRichText(message.message) + "\n");
                            }
                            UpdateMessageTimestamp(message);
                        }
                    }

                    SendBirdSDK.MarkAsRead(messagingChannelUrl);
                    SendBirdSDK.Join(messagingChannelUrl);
                    SendBirdSDK.Connect(GetMaxMessageTimestamp());
                }));
            };
            messageListQuery.Prev(long.MaxValue, 50);
        };
        seh.OnMessagingUpdated += (sender, e) => {
            UpdateMessagingChannel(e.MessagingChannel);
        };
        seh.OnMessagingChannelUpdated += (sender, e) => {
            if (mMessagingChannel != null && mMessagingChannel.GetId() == e.MessagingChannel.GetId())
            {
                mEventProcessor.QueueEvent(new Action(() => {
                    UpdateMessagingChannel(e.MessagingChannel);
                }));
            }
        };

        SendBirdSDK.SetEventHandler(seh);
    }