Beispiel #1
0
        //public static async Task<string> SendMessageAndroid(string Title, string Body, string Content, int? NotifyType,  string Topic, int? FormId, int? ParameterId, string FullUrl,string FullUrlImage, int? ProductTypeId)
        //{
        //    WebRequest tRequest;
        //    //thiết lập FCM send
        //    tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        //    tRequest.Method = "POST";
        //    tRequest.UseDefaultCredentials = true;
        //    tRequest.PreAuthenticate = true;
        //    tRequest.Credentials = CredentialCache.DefaultNetworkCredentials;

        //    //định dạng JSON
        //    tRequest.ContentType = "application/json";
        //    tRequest.Headers.Add(string.Format("Authorization: key={0}", "AIzaSyChBVls7mr2oXkXuIQlkfZze6MZb6sXU3w"));
        //    tRequest.Headers.Add(string.Format("Sender: id={0}", "636270236020"));

        //    // Generate Json
        //    string PostData = GenerateJsonContentToppic(Title, Body, Content, NotifyType, Topic,FormId,ParameterId,FullUrl,FullUrlImage,ProductTypeId);

        //    Byte[] byteArray = Encoding.UTF8.GetBytes(PostData);
        //    tRequest.ContentLength = byteArray.Length;

        //    Stream dataStream = await tRequest.GetRequestStreamAsync();
        //    dataStream.Write(byteArray, 0, byteArray.Length);
        //    dataStream.Close();

        //    WebResponse tResponse = tRequest.GetResponse();

        //    dataStream =  tResponse.GetResponseStream();

        //    StreamReader tReader = new StreamReader(dataStream);

        //    string sResponseFromServer =  tReader.ReadToEnd();

        //    tReader.Close();
        //    dataStream.Close();
        //    tResponse.Close();
        //    return  sResponseFromServer;
        //}
        public static async Task <string> SendMessageFirebase(FCMMessageOutputDTO model)
        {
            if (FirebaseApp.DefaultInstance == null)
            {
                var defaultApp = FirebaseApp.Create(new AppOptions()
                {
                    Credential = GoogleCredential.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FBkey.json")),
                });
            }

            //Convert Data to List Dictionnary
            var jsonData       = JsonConvert.SerializeObject(model.Data);
            var dictionaryData = JsonConvert.DeserializeObject <Dictionary <string, string> >(jsonData);
            var message        = new Message()
            {
                Data         = dictionaryData,
                Notification = new FirebaseAdmin.Messaging.Notification
                {
                    Title = model.Notification.Title,
                    Body  = model.Notification.Body
                },
                // Token = model.Token
                Topic = model.Topic
            };
            var messaging = FirebaseMessaging.DefaultInstance;
            var result    = await messaging.SendAsync(message);

            return(result);
        }
Beispiel #2
0
        public static string GenerateJsonContentToppic(string Title, string Body, string Content, int?NotifyType, string Topic, string FormId, int?ParameterId, string FullUrl, string FullUrlImage, int?ProductTypeId)
        {
            string Result = "";
            FCMMessageOutputDTO FCMMessageV2Output_Item = new FCMMessageOutputDTO();

            FCMMessageV2Output_Item.Notification.Title = Title;
            FCMMessageV2Output_Item.Notification.Body  = Body;
            FCMMessageV2Output_Item.Data.notifyType    = NotifyType ?? 0;
            FCMMessageV2Output_Item.Data.formId        = FormId;
            FCMMessageV2Output_Item.Data.id            = ParameterId ?? 0;
            FCMMessageV2Output_Item.Data.fullUrl       = FullUrl;
            FCMMessageV2Output_Item.Data.fullUrlImage  = FullUrlImage;
            FCMMessageV2Output_Item.Data.typeId        = ProductTypeId;
            Result = JsonConvert.SerializeObject(FCMMessageV2Output_Item);

            return(Result);
        }
Beispiel #3
0
        public async Task <ShoppingCartDetailDTO> PutShopingCartAction(ShoppingCartDetailDTO model)
        {
            var output = new ShoppingCartDetailDTO();
            var result = await _repoWrapper.ShoppingCart.PostShopingCartAction(model);

            if (result)
            {
                output.ErrorCode = "00";
                output.Message   = "Xử lý thành công";
                var shopingCartDetail = await _repoWrapper.ShoppingCart.FirstOrDefaultAsync(x => x.Id == model.Id);

                var product = await _repoWrapper.Product.FirstOrDefaultAsync(x => x.Product_ID == shopingCartDetail.ProductId);

                string message = "";
                string title   = "";
                FCMMessageOutputDTO notiModel = new FCMMessageOutputDTO();
                if (model.StatusCart == 2) // Chờ xác nhận
                {
                    title   = "Xác nhận đơn hàng";
                    message = $"Đơn hàng {shopingCartDetail.ShopingCartCode}, đã được cửa hàng xác nhận";
                    notiModel.Data.formId       = "CART_SELL";
                    notiModel.Topic             = shopingCartDetail.CreateBy.ToString(); // Noti cho người mua
                    notiModel.Data.id           = shopingCartDetail.Id;
                    notiModel.Data.notiSpecType = 0;
                }
                else if (model.StatusCart == 3) //Hoàn thành
                {
                    title   = "Xác nhận đơn hàng hoàn tất";
                    message = $"Đơn hàng {shopingCartDetail.ShopingCartCode}, đã được hoàn tất";
                    notiModel.Data.formId       = "CART_SELL";
                    notiModel.Topic             = shopingCartDetail.CreateBy.ToString(); // Noti cho người mua
                    notiModel.Data.id           = shopingCartDetail.Id;
                    notiModel.Data.notiSpecType = 0;
                }
                else if (model.StatusCart == 4) // Đã hủy
                {
                    if (model.IsCancelByBuyer == 1)
                    {
                        title   = "Đơn hàng bị hủy";
                        message = $"Đơn hàng {shopingCartDetail.ShopingCartCode}, đã hủy bởi người mua - lý do: {model.ReasonCancel}";
                        notiModel.Data.formId       = "CART_BUY";
                        notiModel.Topic             = product?.CreateBy.ToString(); // Thông báo tới gian hàng
                        notiModel.Data.id           = shopingCartDetail.Id;
                        notiModel.Data.notiSpecType = 0;
                    }
                    else if (model.IsCancelByBuyer == 0)
                    {
                        title   = "Đơn hàng bị hủy";
                        message = $"Đơn hàng {shopingCartDetail.ShopingCartCode}, đã hủy bởi người bán - lý do: {model.ReasonCancel}";
                        notiModel.Data.formId       = "CART_SELL";
                        notiModel.Topic             = shopingCartDetail.CreateBy.ToString(); // Noti cho người mua
                        notiModel.Data.id           = shopingCartDetail.Id;
                        notiModel.Data.notiSpecType = 0;
                    }
                }


                notiModel.Notification.Title = title;
                notiModel.Notification.Body  = message;
                //notiModel.Topic = product?.CreateBy.ToString(); // Thông báo tới gian hàng
                var pushNoti = Utils.Util.SendMessageFirebase(notiModel);

                FCMMessage fCMMessage = new FCMMessage();
                fCMMessage.Title            = notiModel.Notification.Title;
                fCMMessage.Body             = notiModel.Notification.Body;
                fCMMessage.CreateBy         = product?.CreateBy;
                fCMMessage.CreateDate       = DateTime.Now;
                fCMMessage.LastEditBy       = product?.CreateBy;
                fCMMessage.LastEditDate     = DateTime.Now;
                fCMMessage.UserID           = product?.CreateBy;
                fCMMessage.Topic            = notiModel.Topic;
                fCMMessage.NotificationType = notiModel.Data.notifyType;
                fCMMessage.Form_ID          = notiModel.Data.formId;
                fCMMessage.ParameterId      = notiModel.Data.id;
                fCMMessage.FullUrl          = notiModel.Data.fullUrl;
                fCMMessage.FullUrlImage     = notiModel.Data.fullUrlImage;
                fCMMessage.NotiSpecType     = 1;
                fCMMessage.IsPinTop         = notiModel.Data.isPinTop;
                fCMMessage.FormNameApp      = notiModel.Data.formAppName;
                fCMMessage.ProductTypeId    = notiModel.Data.typeId;
                fCMMessage.CategoryId       = notiModel.Data.categoryId;
                fCMMessage.FullUrlImage     = $"https://hanoma-cdn.s3.cloud.cmctelecom.vn/DataMobile/Notify/notifyDefault.png";
                fCMMessage.Content          = message;
                fCMMessage.FullUrl          = "";


                fCMMessage.HasRead = 0;
                await _repoWrapper.FCMMessage.AddNewFCMMessage(fCMMessage);
            }
            else
            {
                output.ErrorCode = "01";
                output.Message   = "Có lỗi trong quá trình xử lý đơn hàng";
            }
            return(output);
        }
Beispiel #4
0
        //[Authorize]
        public async Task <ModelBaseStatus> PostShoppingCart(List <PostShoppingCart> model)
        {
            var output = new ModelBaseStatus();
            var result = await _repoWrapper.ShoppingCart.PostShopingCart(model);

            if (result.Count != 0)
            {
                output.ErrorCode = "00";
                output.Message   = "Đặt hàng thành công";
                foreach (var p in result)
                {
                    var product = await _repoWrapper.Product.FirstOrDefaultAsync(x => x.Product_ID == p.ProductId);

                    FCMMessageOutputDTO notiModel = new FCMMessageOutputDTO();
                    notiModel.Notification.Title = $"Đơn hàng được tạo mới";
                    notiModel.Notification.Body  = $"{DateTime.Now.ToString("dd/MM/yyyy")} Đơn hàng {p.ShopingCartCode} vừa được tạo mới";
                    notiModel.Topic             = product?.CreateBy.ToString(); // Thông báo tới gian hàng
                    notiModel.Data.formId       = "CART_BUY";
                    notiModel.Data.id           = p.Id;                         // ShoppingCartDetail ID
                    notiModel.Data.notiSpecType = 0;
                    var pushNoti = Utils.Util.SendMessageFirebase(notiModel);

                    FCMMessage fCMMessage = new FCMMessage();
                    fCMMessage.Title            = notiModel.Notification.Title;
                    fCMMessage.Body             = notiModel.Notification.Body;
                    fCMMessage.CreateBy         = product?.CreateBy;
                    fCMMessage.CreateDate       = DateTime.Now;
                    fCMMessage.LastEditBy       = product?.CreateBy;
                    fCMMessage.LastEditDate     = DateTime.Now;
                    fCMMessage.UserID           = product?.CreateBy;
                    fCMMessage.Topic            = product?.CreateBy.ToString(); // Thông báo tới gian hàng
                    fCMMessage.NotificationType = notiModel.Data.notifyType;
                    fCMMessage.Form_ID          = notiModel.Data.formId;
                    fCMMessage.ParameterId      = notiModel.Data.id;
                    fCMMessage.FullUrl          = notiModel.Data.fullUrl;
                    fCMMessage.FullUrlImage     = notiModel.Data.fullUrlImage;
                    fCMMessage.NotiSpecType     = 1;
                    fCMMessage.IsPinTop         = notiModel.Data.isPinTop;
                    fCMMessage.FormNameApp      = notiModel.Data.formAppName;
                    fCMMessage.ProductTypeId    = notiModel.Data.typeId;
                    fCMMessage.CategoryId       = notiModel.Data.categoryId;
                    fCMMessage.FullUrlImage     = $"https://hanoma-cdn.s3.cloud.cmctelecom.vn/DataMobile/Notify/notifyDefault.png";
                    fCMMessage.Content          = $"{DateTime.Now.ToString("dd/MM/yyyy")} Đơn hàng {p.ShopingCartCode} vừa được tạo mới";
                    fCMMessage.FullUrl          = "";


                    fCMMessage.HasRead = 0;
                    await _repoWrapper.FCMMessage.AddNewFCMMessage(fCMMessage);
                }
            }
            else
            {
                output.ErrorCode = "01";
                output.Message   = "Có lỗi trong quá trình đặt hàng";
                FCMMessageOutputDTO notiModel = new FCMMessageOutputDTO();
                notiModel.Notification.Title = "Có lỗi trong quá trình đặt hàng";
                notiModel.Notification.Body  = "Có lỗi trong quá trình đặt hàng";
                notiModel.Topic             = model[0].UserId; // Thông báo tới chính người dùng
                notiModel.Data.formId       = "SHOPPINGCARTNSS";
                notiModel.Data.notiSpecType = 0;
                var pushNoti = Utils.Util.SendMessageFirebase(notiModel);

                FCMMessage fCMMessage = new FCMMessage();
                fCMMessage.Title            = notiModel.Notification.Title;
                fCMMessage.Body             = notiModel.Notification.Body;
                fCMMessage.CreateBy         = Guid.Parse(model[0].UserId);
                fCMMessage.CreateDate       = DateTime.Now;
                fCMMessage.LastEditBy       = Guid.Parse(model[0].UserId);
                fCMMessage.LastEditDate     = DateTime.Now;
                fCMMessage.UserID           = Guid.Parse(model[0].UserId);
                fCMMessage.Topic            = model[0].UserId; // Thông báo tới chính người dùng
                fCMMessage.NotificationType = notiModel.Data.notifyType;
                fCMMessage.Form_ID          = notiModel.Data.formId;
                fCMMessage.ParameterId      = notiModel.Data.id;
                fCMMessage.FullUrl          = notiModel.Data.fullUrl;
                fCMMessage.FullUrlImage     = notiModel.Data.fullUrlImage;
                fCMMessage.NotiSpecType     = 1;
                fCMMessage.IsPinTop         = notiModel.Data.isPinTop;
                fCMMessage.FormNameApp      = notiModel.Data.formAppName;
                fCMMessage.ProductTypeId    = notiModel.Data.typeId;
                fCMMessage.CategoryId       = notiModel.Data.categoryId;
                fCMMessage.FullUrlImage     = $"https://hanoma-cdn.s3.cloud.cmctelecom.vn/DataMobile/Notify/notifyDefault.png";
                fCMMessage.Content          = "Có lỗi trong quá trình đặt hàng";
                fCMMessage.FullUrl          = "";


                fCMMessage.HasRead = 0;
                await _repoWrapper.FCMMessage.AddNewFCMMessage(fCMMessage);
            }
            return(output);
        }