public override void OnMessageReceived(string from, Bundle data)
        {
            var messageStr          = data.GetString("message");
            BitopiGcmMessage gcmMsg = JsonConvert.DeserializeObject <BitopiGcmMessage>(messageStr);

            SendNotification(gcmMsg);
        }
Example #2
0
        void SendNotification(BitopiGcmMessage gcmMsg, string DeviceToken)
        {
            Dictionary <string, List <string> > ReceivingMessages;
            string            s = "";
            BitopiApplication bitopiApplication = (BitopiApplication)this.ApplicationContext;

            try
            {
                s = Newtonsoft.Json.JsonConvert.SerializeObject(gcmMsg);
                if (PushNotificationSingleton.Instance.ReadNotification() == "")
                {
                    ReceivingMessages = new Dictionary <string, List <string> >();
                }
                else
                {
                    string notiMsg = PushNotificationSingleton.Instance.ReadNotification();
                    ReceivingMessages = JsonConvert.DeserializeObject <Dictionary <string, List <string> > >(notiMsg);
                }
                string title = "", msgBody = "";

                // TytMoblieApplication application = (TytMoblieApplication)this.ApplicationContext;
                //BitopiGcmMessage gcmMsg = JsonConvert.DeserializeObject<BitopiGcmMessage>(message);
                string approvalName = gcmMsg.Approval == "1" ? "PO" :
                                      gcmMsg.Approval == "4" ? "Cash Requisition" : gcmMsg.Approval == "5" ? "Express Cash Requisition" : "";
                int requestID = Convert.ToInt16(gcmMsg.Approval);
                if (ReceivingMessages.Count > 0 && ReceivingMessages.ContainsKey(gcmMsg.Approval))
                {
                    ReceivingMessages[gcmMsg.Approval].Add(gcmMsg.POID);
                    msgBody = "You have " + ReceivingMessages[gcmMsg.Approval].Count() + " " + gcmMsg.ApprovalName + " to " + gcmMsg.ApprovalType;
                }
                else
                {
                    ReceivingMessages.Add(gcmMsg.Approval, new List <string> {
                        gcmMsg.POID
                    });
                    msgBody = "You have 1 " + gcmMsg.ApprovalName + " to " + gcmMsg.ApprovalType;
                }
                var totalNotification = 0;
                foreach (var msg in ReceivingMessages)
                {
                    totalNotification += msg.Value.Count;
                }


                try
                {
                    CrossBadge.Current.SetBadge(totalNotification);
                }
                catch { }

                //int result = repo.ReceiveNotification(bitopiApplication.User.UserCode,
                //    bitopiApplication.MacAddress,(ApprovalType)Convert.ToInt32(gcmMsg.Approval), gcmMsg.POID).Result;

                string approval = ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.PurchaseOrderApproval) ? "PO" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.CashRequisition) ? "Cash Requisition" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.LeaveApplication) ? "Leave Application" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.ChequeRequisitionInformation) ? "ChequeRequisition" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.ExpressCashRequisition) ? "ExpressCashRequisition" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.PORequisitionApproval) ? "PO Requisition" :
                                  ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.UnSeenTask) ? "UnSeenTask" : "";
                ApprovalRepository repo = new ApprovalRepository();
                string             url  = RepositorySettings.BaseURl + "Notification?userId=" +
                                          Cipher.Encrypt(bitopiApplication.User.UserCode) + "&deviceId=" +
                                          DeviceToken
                                          + "&approval=" + gcmMsg.ApprovalName + "&requisitioId="
                                          + gcmMsg.POID;

                HttpClient          client = new HttpClient();
                HttpResponseMessage result = client.GetAsync(url).Result;
                int count = JsonConvert.DeserializeObject <int>(result.Content.ReadAsStringAsync().Result);

                gcmMsg = new BitopiGcmMessage();
                //ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(this);
                //string employeeid = sp.GetString("employeeid_id", "");

                gcmMsg.DateCreated = DateTime.Now;
                Intent intent; intent = new Intent(this, typeof(StartupActivity));
                if ((ApprovalType)Convert.ToInt32(gcmMsg.Approval) == ApprovalType.UnSeenTask)
                {
                    intent = new Intent(this, typeof(MyTaskMenu));
                }
                intent.AddFlags(ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(ApplicationContext, requestID, intent, 0);//requestID = DateTime.Now.Millisecond;
                var notificationBuilder = new NotificationCompat.Builder(this)
                                          .SetSmallIcon(BitopiApprovalSystem.Resource.Drawable.bitopiLogo)
                                          .SetContentTitle("Bitopi Approval System")
                                          .SetContentText(msgBody)
                                          .SetAutoCancel(true)
                                          .SetContentIntent(pendingIntent)
                                          .SetDefaults(NotificationCompat.DefaultSound);

                string toSaveNotiMsg = Newtonsoft.Json.JsonConvert.SerializeObject(ReceivingMessages);
                PushNotificationSingleton.Instance.SaveNotification(toSaveNotiMsg);
                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);

                //new UserRepository().GetNotificationCacheAsCompleted(gcmMsg.NotificationExecId);
                notificationManager.Notify(requestID, notificationBuilder.Build());
            }
            catch (Exception ex)
            {
                //CustomLogger.CustomLog("Line:" + Line + " From Activity: " + bitopiApplication.CurrentActivity + "\nMessage: " + s + "\nStack Trace: " + ex.StackTrace + "\n\n", "", bitopiApplication.User != null ?
                //         bitopiApplication.User.UserName : "");
            }
        }
        void SendNotification(BitopiGcmMessage gcmMsg)
        {
            try
            {
                string title = "", msgBody = "";

                // TytMoblieApplication application = (TytMoblieApplication)this.ApplicationContext;
                //BitopiGcmMessage gcmMsg = JsonConvert.DeserializeObject<BitopiGcmMessage>(message);
                string approvalName = gcmMsg.Approval == "1" ? "PO" :
                                      gcmMsg.Approval == "4" ? "Cash Requisition" : "";
                int requestID = Convert.ToInt16(gcmMsg.Approval);
                if (BitopiSingelton.Instance.ReceivingMessages.ContainsKey(gcmMsg.Approval))
                {
                    BitopiSingelton.Instance.ReceivingMessages[gcmMsg.Approval].Add(gcmMsg.POID);
                    msgBody = "You have " + BitopiSingelton.Instance.ReceivingMessages[gcmMsg.Approval].Count() + " " + approvalName + " to " + gcmMsg.ApprovalType;
                }
                else
                {
                    BitopiSingelton.Instance.ReceivingMessages.Add(gcmMsg.Approval, new List <string> {
                        gcmMsg.POID
                    });
                    msgBody = "You have 1 " + approvalName + " to " + gcmMsg.ApprovalType;
                }
                ApprovalRepository repo = new ApprovalRepository();
                int result = repo.ReceiveNotification(BitopiSingelton.Instance.User.UserCode,
                                                      BitopiSingelton.Instance.MacAddress, (ApprovalType)Convert.ToInt32(gcmMsg.Approval), gcmMsg.ApprovalName, gcmMsg.POID).Result;
                gcmMsg = new BitopiGcmMessage();
                //ISharedPreferences sp = PreferenceManager.GetDefaultSharedPreferences(this);
                //string employeeid = sp.GetString("employeeid_id", "");

                gcmMsg.DateCreated = DateTime.Now;
                Intent intent;


                intent = new Intent(this, typeof(StartupActivity));


                intent.AddFlags(ActivityFlags.ClearTop);

                var pendingIntent = PendingIntent.GetActivity(ApplicationContext, requestID, intent, 0);



                //requestID = DateTime.Now.Millisecond;
                var notificationBuilder = new NotificationCompat.Builder(this)
                                          .SetSmallIcon(BitopiApprovalSystem.Resource.Drawable.bitopiLogo)
                                          .SetContentTitle("Bitopi Approval System")
                                          .SetContentText(msgBody)
                                          .SetAutoCancel(true)
                                          .SetContentIntent(pendingIntent)
                                          .SetDefaults(NotificationCompat.DefaultSound);


                var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);

                //new UserRepository().GetNotificationCacheAsCompleted(gcmMsg.NotificationExecId);
                notificationManager.Notify(requestID, notificationBuilder.Build());
            }
            catch (Exception ex)
            {
                CustomLogger.CustomLog("From Activity: " + BitopiSingelton.Instance.CurrentActivity + "\nMessage: " + ex.Message + "\nStack Trace: " + ex.StackTrace + "\n\n", "", BitopiSingelton.Instance.User != null ?
                                       BitopiSingelton.Instance.User.UserName : "");
            }
        }