private void Completed(object sender, AsyncCompletedEventArgs e) { timer.Stop(); timer.Dispose(); notificationManager.Cancel(notificationID); notificationID = new System.Random().Next(10000, 99999); notificationBuilder = new Notification.Builder(ApplicationContext); notificationBuilder.SetOngoing(false) .SetSmallIcon(Resource.Drawable.icon); if (e.Cancelled == false && e.Error == null) { notificationBuilder.SetContentTitle("Download complete"); } else { notificationBuilder.SetContentTitle("Upload failed"); } notification = notificationBuilder.Build(); notificationManager.Notify(notificationID, notification); notificationID = 0; notificationBuilder.Dispose(); notification.Dispose(); notificationManager.Dispose(); uploadedBytes = 0; totalBytes = 0; percentComplete = 0; wClient.Dispose(); }
private void NotifyToAutofill(string uri, NotificationManager notificationManager) { if (notificationManager == null || string.IsNullOrWhiteSpace(uri)) { return; } var intent = new Intent(this, typeof(AutofillActivity)); intent.PutExtra("uri", uri); intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent); var builder = new Notification.Builder(this); builder.SetSmallIcon(Resource.Drawable.notification_sm) .SetContentTitle(App.Resources.AppResources.BitwardenAutofillService) .SetContentText(App.Resources.AppResources.BitwardenAutofillServiceNotificationContent) .SetTicker(App.Resources.AppResources.BitwardenAutofillServiceNotificationContent) .SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis()) .SetContentIntent(pendingIntent); if (Build.VERSION.SdkInt > BuildVersionCodes.KitkatWatch) { builder.SetVisibility(NotificationVisibility.Secret) .SetColor(global::Android.Support.V4.Content.ContextCompat.GetColor(ApplicationContext, Resource.Color.primary)); } notificationManager.Notify(AutoFillNotificationId, builder.Build()); builder.Dispose(); }
private void NotifyToAutofill(string uri, NotificationManager notificationManager) { if (notificationManager == null || string.IsNullOrWhiteSpace(uri)) { return; } var now = Java.Lang.JavaSystem.CurrentTimeMillis(); var intent = new Intent(this, typeof(AutofillActivity)); intent.PutExtra("uri", uri); intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.SingleTop | ActivityFlags.ClearTop); var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent); var notificationContent = Build.VERSION.SdkInt > BuildVersionCodes.KitkatWatch ? AppResources.BitwardenAutofillServiceNotificationContent : AppResources.BitwardenAutofillServiceNotificationContentOld; var builder = new Notification.Builder(this); builder.SetSmallIcon(Resource.Drawable.notification_sm) .SetContentTitle(AppResources.BitwardenAutofillService) .SetContentText(notificationContent) .SetTicker(notificationContent) .SetWhen(now) .SetContentIntent(pendingIntent); if (Build.VERSION.SdkInt > BuildVersionCodes.KitkatWatch) { builder.SetVisibility(NotificationVisibility.Secret) .SetColor(global::Android.Support.V4.Content.ContextCompat.GetColor(ApplicationContext, Resource.Color.primary)); } if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { if (_notificationChannel == null) { _notificationChannel = new NotificationChannel("bitwarden_autofill_service", AppResources.AutofillService, NotificationImportance.Low); notificationManager.CreateNotificationChannel(_notificationChannel); } builder.SetChannelId(_notificationChannel.Id); } if (/*Build.VERSION.SdkInt <= BuildVersionCodes.N && */ _appSettings.AutofillPersistNotification) { builder.SetPriority(-2); } _lastNotificationTime = now; _lastNotificationUri = uri; notificationManager.Notify(AutoFillNotificationId, builder.Build()); builder.Dispose(); }
private void WClient_UploadFileCompleted(object sender, System.Net.UploadFileCompletedEventArgs e) { timer.Stop(); timer.Dispose(); notificationManager.Cancel(notificationID); notificationID = new Random().Next(10000, 99999); notificationBuilder = new Notification.Builder(ApplicationContext); notificationBuilder.SetOngoing(false) .SetSmallIcon(Resource.Drawable.icon); if (e.Cancelled == false && e.Error == null) { JsonResult result = JsonConvert.DeserializeObject <JsonResult>(System.Text.Encoding.UTF8.GetString(e.Result)); if (result != null && result.Success == true) { notificationBuilder.SetContentTitle("Upload complete"); } else if (result.Success == false) { notificationBuilder.SetContentTitle("Upload failed"); Intent notificationIntent = new Intent(ApplicationContext, typeof(UploadErrorActivity)); PendingIntent contentIntent = PendingIntent.GetActivity(ApplicationContext, 0, notificationIntent, PendingIntentFlags.UpdateCurrent); notificationBuilder.SetContentIntent(contentIntent); notificationBuilder.SetContentText(result.Message); notificationBuilder.SetAutoCancel(true); } } else { notificationBuilder.SetContentTitle("Upload failed"); } notification = notificationBuilder.Build(); notificationManager.Notify(notificationID, notification); notificationID = 0; notificationBuilder.Dispose(); notification.Dispose(); notificationManager.Dispose(); uploadedBytes = 0; totalBytes = 0; percentComplete = 0; wClient.Dispose(); }
/// <summary> /// 下载进度 /// </summary> /// <param name="title">标题</param> /// <param name="max">最大进度</param> /// <param name="progress">当前进度</param> public void DownloadProgress(string title, int max, int progress) { // 获取系统全局Notification管理器 if (Settings.nm == null) { Settings.nm = this.GetSystemService(Context.NotificationService) as NotificationManager; } if (progress > max) { Settings.nm.Cancel(0); // 下载完毕,移除通知栏 } else { int requestCode = 123456 + System.DateTime.Now.Millisecond; using (Intent intent = new Intent()) { using (PendingIntent operation = PendingIntent.GetActivity(this, requestCode, intent, PendingIntentFlags.OneShot)) { Notification notification = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.Icon) // App小图标 .SetLargeIcon(null) // App大图标 .SetContentTitle(title) // 设置通知信息 .SetContentIntent(operation) .SetWhen(DateTime.Now.Ticks) .SetContentText("当前下载进度: " + progress / max + "%") .SetAutoCancel(true) // 用户点击后自动删除 .SetDefaults(NotificationDefaults.Lights) // 灯光 .SetPriority(0) // 设置优先级 .SetOngoing(true) .SetProgress(max, progress, false) // max最大进度 progress当前进度 .Build(); Settings.nm.Notify(0, notification); notification.Dispose(); notification = null; } } } }
private async Task UploadSingleFile(MediaFile file) { Uri uri; if (file.MediaType == Core.Model.MediaType.Photo) { uri = new Uri(GlobalClass.BaseUrl + "Event/UploadEventFiles?eventid=" + file.EventID); } else { uri = new Uri(GlobalClass.BaseUrl + "Event/UploadVideo?eventid=" + file.EventID); } string uploadFilePath = file.FilePath; Java.IO.File mediaFile = new Java.IO.File(uploadFilePath); totalBytes = mediaFile.Length(); if (totalBytes == 0) { return; } double totalMB = Convert.ToDouble(totalBytes) / (1024 * 1024); // Size in MB if (totalMB > MaxVideoSize) { notificationBuilder = new Notification.Builder(ApplicationContext); notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationBuilder.SetOngoing(false) .SetSmallIcon(Resource.Drawable.icon); notificationBuilder.SetContentTitle("Upload failed"); Intent notificationIntent = new Intent(ApplicationContext, typeof(UploadErrorActivity)); PendingIntent contentIntent = PendingIntent.GetActivity(ApplicationContext, 0, notificationIntent, PendingIntentFlags.UpdateCurrent); notificationBuilder.SetContentIntent(contentIntent); notificationBuilder.SetContentText("Video size is too large"); notificationBuilder.SetAutoCancel(true); notification = notificationBuilder.Build(); notificationID = new Random().Next(10000, 99999); notificationManager.Notify(notificationID, notification); notificationID = 0; notificationBuilder.Dispose(); notification.Dispose(); notificationManager.Dispose(); return; } else // && (totalBytes / 1024) > 500 if (file.MediaType == Core.Model.MediaType.Photo) { uploadFilePath = CompressFile.compressImage(uploadFilePath); mediaFile = new Java.IO.File(uploadFilePath); totalBytes = mediaFile.Length(); } uploadedBytes = 0; percentComplete = 0; var totalfileSize = Math.Round(totalMB, 2); notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationBuilder = new Notification.Builder(ApplicationContext); notificationBuilder.SetOngoing(true) .SetSmallIcon(Resource.Drawable.ic_file_upload_white_18dp) .SetContentTitle("Uploading") .SetContentText (Math.Round((Convert.ToDecimal(uploadedBytes) / (1024.0m * 1024.0m)), 2) + " MB of " + totalfileSize + " MB") .SetProgress(100, percentComplete, false); notification = notificationBuilder.Build(); notificationID = new Random().Next(10000, 99999); notificationManager.Notify(notificationID, notification); try { timer = new System.Timers.Timer(); timer.Elapsed += Timer_Elapsed; timer.Interval = 1000; timer.Enabled = true; timer.Start(); } catch (Exception ex) { throw; } wClient = new WebClient(); wClient.Headers.Add("authorization", "bearer " + GlobalClass.AccessToken); wClient.UploadFileAsync(uri, uploadFilePath); wClient.UploadProgressChanged += new UploadProgressChangedEventHandler(ProgressChanged); wClient.UploadFileCompleted += WClient_UploadFileCompleted; }