コード例 #1
0
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            new Task(() =>
            {
                System.Timers.Timer timer = new System.Timers.Timer();
                //ApprovalRepository repo = new ApprovalRepository();
                timer.Interval = 10000;
                timer.Elapsed += (s, e) =>
                {
                    Intent notiIntent = null;

                    BitopiApplication bitopiApplication = (BitopiApplication)this.ApplicationContext;
                    try
                    {
                        if (bitopiApplication.User == null || String.IsNullOrEmpty(bitopiApplication.User.UserCode) == true)
                        {
                            ISharedPreferences pref      = Application.Context.GetSharedPreferences("_bitopi_UserInfo", FileCreationMode.Private);
                            string _userCode             = pref.GetString("UserCode", String.Empty);
                            ISharedPreferences prefToken = Application.Context.GetSharedPreferences("_bitopi_DeviceToken", FileCreationMode.Private);
                            string Token = prefToken.GetString("Token", String.Empty);
                            if (String.IsNullOrEmpty(Token))
                            {
                                bitopiApplication.MacAddress = Guid.NewGuid().ToString();
                                prefToken.Edit().PutString("Token", bitopiApplication.MacAddress).Commit();
                            }
                            else
                            {
                                bitopiApplication.MacAddress = Token;
                            }
                            if (_userCode == "")
                            {
                                notiIntent = new Intent(this, typeof(LoginActivity));
                            }
                            else
                            {
                                notiIntent             = new Intent(this, typeof(BitopiActivity));
                                bitopiApplication.User = new UserModel()
                                {
                                    UserCode = _userCode
                                };
                                string url = RepositorySettings.BaseURl + "Notification?UserID=" + Cipher.Encrypt(bitopiApplication.User.UserCode)
                                             + "&DeviceID=" + bitopiApplication.MacAddress;

                                HttpClient client          = new HttpClient();
                                HttpResponseMessage result = client.GetAsync(url).Result;
                                var messages = JsonConvert.DeserializeObject <List <BitopiGcmMessage> >(result.Content.ReadAsStringAsync().Result);
                                if (messages != null && messages.Count > 0)
                                {
                                    SendNotification(messages.First(), bitopiApplication.MacAddress);
                                }
                            }
                        }
                        else
                        {
                            notiIntent = new Intent(this, typeof(BitopiActivity));
                        }

                        int UpdateVersion     = new AccountRepository().GetVersion().Result;
                        int lastUpdateVersion = DBAccess.Database.LastVersion();
                        if (lastUpdateVersion == 0)
                        {
                            lastUpdateVersion = this.PackageManager.GetPackageInfo(this.PackageName,
                                                                                   Android.Content.PM.PackageInfoFlags.MetaData).VersionCode;
                            DBAccess.Database.InsertVersion(lastUpdateVersion);
                        }
                        if (UpdateVersion > lastUpdateVersion)
                        {
                            int requestID = DateTime.Now.Millisecond;
                            DBAccess.Database.DeleteVersion(lastUpdateVersion);
                            DBAccess.Database.InsertVersion(UpdateVersion);
                            notiIntent = new Intent(this, typeof(DownloadNewVersionActivity));
                            notiIntent.AddFlags(ActivityFlags.ClearTop);
                            var pendingIntent       = PendingIntent.GetActivity(ApplicationContext, requestID, notiIntent, 0);//requestID = DateTime.Now.Millisecond;
                            var notificationBuilder = new NotificationCompat.Builder(this)
                                                      .SetSmallIcon(BitopiApprovalSystem.Resource.Drawable.bitopiLogo)
                                                      .SetContentTitle("Bitopi Approval System")
                                                      .SetContentText("New Version Arrived")
                                                      .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: " + Line + "\nMessage: " + ex.Message + "\nStack Trace: " + ex.StackTrace + "\n\n", "", bitopiApplication.User != null ?
                        //   bitopiApplication.User.UserName : "");
                    }
                };
                timer.Enabled = true;
            }).Start();
            return(base.OnStartCommand(intent, flags, startId));
        }
コード例 #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 : "");
            }
        }