コード例 #1
0
        public override Result DoWork()
        {
            var logger = new OurLoggerService();
            var workNo = InputData.GetInt("WORK_NO", 0);

            logger.LogInformation($"Work NO: {workNo} Started");

            AlarmManager         alarmManager = MainActivity.Instance.GetSystemService(Context.AlarmService) as AlarmManager;
            List <PendingIntent> intentArray  = new List <PendingIntent>();

            Bundle notificationDataBundle = new Bundle();

            notificationDataBundle.PutString("Title", "Serilog File Notification");
            notificationDataBundle.PutString("Message", $"Showing serilog file notification for task {workNo}");
            notificationDataBundle.PutInt("WORK_NO", workNo);

            Intent notificationIntent = new Intent(MainActivity.Instance, typeof(NotificationAlertReceiver));

            notificationIntent.PutExtra("NotificationData", notificationDataBundle);
            var random        = new Random();
            var randomSeconds = random.Next(20, 60);

            int           alarmRequestCode = workNo * 10 + workNo;
            PendingIntent alarmIntent      = PendingIntent.GetBroadcast(
                MainActivity.Instance,
                alarmRequestCode,
                notificationIntent,
                PendingIntentFlags.Immutable
                );

            TimeSpan myTimeSpan   = new TimeSpan(0, 0, randomSeconds);
            DateTime scheduleTime = DateTime.Now + myTimeSpan;

            logger.LogInformation($"Setting Alarm for {workNo} at {scheduleTime}");

            long alarmTimeEpoch   = new DateTimeOffset(scheduleTime).ToUnixTimeMilliseconds();
            long currentTimeEpoch = new DateTimeOffset(DateTime.Now).ToUnixTimeMilliseconds();

            if (alarmTimeEpoch > currentTimeEpoch)
            {
                alarmManager.SetAlarmClock(
                    new AlarmManager.AlarmClockInfo(
                        alarmTimeEpoch,
                        alarmIntent
                        ),
                    alarmIntent
                    );

                intentArray.Add(alarmIntent);
            }

            logger.LogInformation($"Work NO: {workNo} set up complete");

            return(Result.InvokeSuccess());
        }
        public override void OnReceive(Context context, Intent intent)
        {
            var logger     = new OurLoggerService();
            var dataBundle = intent.GetBundleExtra("NotificationData");
            var workNo     = dataBundle.GetInt("WORK_NO", 0);

            logger.LogInformation($"Notification Recived for Work NO: {workNo}");
            MainActivity.NotificationBuildAndShow(context, intent);
        }
コード例 #3
0
        public static void NotificationBuildAndShow(Context context, Intent intent)
        {
            var dataBundle = intent.GetBundleExtra("NotificationData");
            var message    = dataBundle.GetString("Message") ?? "My Message";
            var title      = dataBundle.GetString("Title") ?? "My Title";
            var workNo     = dataBundle.GetInt("WORK_NO", 0);
            var logger     = new OurLoggerService();

            logger.LogInformation($"Building and Showing Notification for Work {workNo}");

            var resultIntent = new Intent(context, typeof(MainActivity));

            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(context, 0, resultIntent, PendingIntentFlags.UpdateCurrent);

            var importance = NotificationImportance.High;
            NotificationChannel notificationChannel = new NotificationChannel(APP_CHANNEL, "Important", importance);

            notificationChannel.EnableVibration(true);
            notificationChannel.LockscreenVisibility = NotificationVisibility.Public;

            var audioAttributes = new AudioAttributes.Builder()
                                  .SetContentType(AudioContentType.Sonification)
                                  .SetUsage(AudioUsageKind.Alarm)
                                  .Build();

            notificationChannel.SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Ringtone), audioAttributes);

            var notificationBuilder = new NotificationCompat.Builder(context, APP_CHANNEL)
                                      .SetSmallIcon(Resource.Mipmap.icon_round)
                                      .SetContentTitle(title)
                                      .SetContentText(message)
                                      .SetContentIntent(pendingIntent)
                                      .SetAutoCancel(false)
                                      .SetChannelId(APP_CHANNEL);

            ;

            NotificationManager notificationManager = (NotificationManager)context.GetSystemService(NotificationService);

            notificationManager.CreateNotificationChannel(notificationChannel);
            notificationManager.Notify(6461, notificationBuilder.Build());
            logger.LogInformation($"Showing Notification for Work {workNo} Complete");
        }