コード例 #1
0
 public MessageController(
     MessageService service,
     OneSignalService notificationService,
     SendBirdService chatService,
     UserService userService
     )
 {
     _messageService      = service;
     _notificationService = notificationService;
     _chatService         = chatService;
     _userService         = userService;
 }
コード例 #2
0
        private void paSendMessage_Execute(object sender, PopupWindowShowActionExecuteEventArgs e)
        {
            var customerMessage = (NPCustomerMessage)e.PopupWindowViewCurrentObject;
            OneSignalService oneSignalService = new OneSignalService();

            foreach (Customer customer in e.SelectedObjects)
            {
                var notificationToken = customer.NotificationToken;
                var title             = customerMessage.Header;
                var content           = customerMessage.Message;
                if (!string.IsNullOrWhiteSpace(notificationToken))
                {
                    oneSignalService.SendToClient(notificationToken, title,
                                                  content);
                    var message = ObjectSpace.CreateObject <Message>();
                    message.Date    = DateTime.Now;
                    message.To      = customer;
                    message.Title   = title;
                    message.Content = content;
                }
            }

            ObjectSpace.CommitChanges();
        }
コード例 #3
0
        public async Task <ApiResponder> SendMessage(string userName, string deviceID,
                                                     string senderUserId, string toUserId, string message,
                                                     string messageKey, ChatType chatType = ChatType.Text)
        {
            try
            {
                Console.WriteLine("sending a message: " + message);

                var newMessage = new Message()
                {
                    ApplicationUserId = Convert.ToInt32(senderUserId),
                    ReceiverId        = Convert.ToInt32(toUserId),
                    Text     = message,
                    CreateAt = DateTime.UtcNow,
                    Type     = chatType
                };

                var itemUser = users.Find(s => s.UserName == toUserId);

                if (itemUser != null)
                {
                    Clients.Client(itemUser.ConnectionID).messageReceived(newMessage);
                }

                var pushNotification = new OneSignalService();
                pushNotification.PushNotification(newMessage, userName, deviceID, senderUserId);


                var conversatition = DataContext.Converstation.Where(s => (s.SenderId == Convert.ToInt32(senderUserId) && s.RecevierId == Convert.ToInt32(toUserId)) || (s.SenderId == Convert.ToInt32(toUserId) && s.RecevierId == Convert.ToInt32(senderUserId))).FirstOrDefault();

                if (conversatition == null)
                {
                    var conversationItem = new Converstation()
                    {
                        SenderId     = Convert.ToInt32(senderUserId),
                        RecevierId   = Convert.ToInt32(toUserId),
                        LastMessage  = message,
                        LastDate     = DateTime.UtcNow,
                        TotalMessage = 1,
                    };

                    DataContext.Converstation.Add(conversationItem);
                }
                else
                {
                    conversatition.TotalMessage = conversatition.TotalMessage + 1;
                    conversatition.LastDate     = DateTime.UtcNow;
                    conversatition.LastMessage  = message;

                    DataContext.Converstation.Update(conversatition);
                }

                DataContext.Message.Add(newMessage);

                await DataContext.SaveChangesAsync();

                var messageReturn = new MessageReturnModel()
                {
                    ApplicationUserId = newMessage.ApplicationUserId,
                    ReceiverId        = newMessage.ReceiverId,
                    Text       = newMessage.Text,
                    CreateAt   = DateTime.UtcNow,
                    Type       = chatType,
                    MessageKey = messageKey,
                };

                var sucessMessage = new ApiResponder(messageReturn, null);

                Console.WriteLine("send Successfully");

                return(sucessMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error" + ex.Message);
                var errorMessage = new ApiResponder(null, new Error()
                {
                    errorCode = 5001, errorMessage = new string[] { ex.Message }
                });
                return(errorMessage);
            }
        }
 public NotificationController()
 {
     oneSignalService = new OneSignalService();
 }