You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.8 KiB

  1. using Android.App;
  2. using Android.Content;
  3. using Android.OS;
  4. using GMCabsDriverAssistant.Services;
  5. using GMCabsDriverAssistantSolution.Platforms.Android;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. [assembly: Dependency(typeof(AndroidAlarmReceiver))]
  12. namespace GMCabsDriverAssistantSolution.Platforms.Android
  13. {
  14. [BroadcastReceiver(Name = "au.com.gmcabs.driverassistant.app.AndroidAlarmReceiver", Enabled = true, Exported = true, DirectBootAware = true)]
  15. public class AndroidAlarmReceiver : BroadcastReceiver, IAlarmService
  16. {
  17. [Obsolete]
  18. public override void OnReceive(Context context, Intent intent)
  19. {
  20. string foregroundChannelId = "GMCabsForeGroundServiceRunningChannel";
  21. var message = "Foreground Service Is On";
  22. var title = "Service Status";
  23. var resultIntent = new Intent(context, typeof(MainActivity));
  24. resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
  25. var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
  26. ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
  27. : PendingIntentFlags.UpdateCurrent;
  28. var pending = PendingIntent.GetActivity(context, 0,
  29. resultIntent,
  30. pendingIntentFlags);
  31. var builder =
  32. new Notification.Builder(context)
  33. .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
  34. //.SetColor(global::Android.App.Application.Context.GetColor(Resource.Color.color_notification_icon))
  35. .SetContentTitle(title)
  36. .SetContentText(message)
  37. .SetPriority((int)NotificationImportance.Max)
  38. .SetAutoCancel(true)
  39. .SetTimeoutAfter(60000);
  40. builder.SetContentIntent(pending);
  41. var notification = builder.Build();
  42. var notifManager = global::Android.App.Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager;
  43. if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.O)
  44. {
  45. var notificationChannel = new NotificationChannel(foregroundChannelId, "GMCabsAlarmManagerNotification", NotificationImportance.Low);
  46. notificationChannel.Importance = NotificationImportance.Low;
  47. notificationChannel.EnableLights(true);
  48. notificationChannel.EnableVibration(true);
  49. notificationChannel.SetShowBadge(true);
  50. notificationChannel.SetVibrationPattern(new long[] { 100, 200, 300 });
  51. if (builder != null)
  52. {
  53. builder.SetChannelId(foregroundChannelId);
  54. notifManager.CreateNotificationChannel(notificationChannel);
  55. }
  56. }
  57. notifManager.Notify(DateTime.Now.Millisecond, notification);
  58. }
  59. public void StartMyAlarmService()
  60. {
  61. var alarmIntent = new Intent(global::Android.App.Application.Context, typeof(AndroidAlarmReceiver));
  62. var pendingIntentFlags = (Build.VERSION.SdkInt >= BuildVersionCodes.S)
  63. ? PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Mutable
  64. : PendingIntentFlags.UpdateCurrent;
  65. var pending = PendingIntent.GetBroadcast(global::Android.App.Application.Context, 0, alarmIntent, pendingIntentFlags);
  66. var alarmManager = (AlarmManager)global::Android.App.Application.Context.GetSystemService(Context.AlarmService);
  67. alarmManager.Set(AlarmType.ElapsedRealtime, SystemClock.ElapsedRealtime() + 1 * 1000, pending);
  68. }
  69. }
  70. }