Example #1
0
        public override void OnReceive(Context context, Intent intent)
        {
            try
            {
                Android.Net.Uri alert = RingtoneManager.GetDefaultUri(RingtoneType.Alarm);
                alert = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
                alert = RingtoneManager.GetDefaultUri(RingtoneType.Ringtone);

                // Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();
                var             matakuliah = intent.GetStringExtra("matakuliah");
                var             waktu      = intent.GetStringExtra("waktu");
                var             jadwalId   = Convert.ToInt32(intent.GetStringExtra("jadwalId"));
                Android.Net.Uri soundUri   = Android.Net.Uri.Parse("android.resource://" + "com.ocph23.stimik.simak" + "/raw/alarm");

                if (Build.VERSION.SdkInt < BuildVersionCodes.O)
                {
                    intent.AddFlags(ActivityFlags.ClearTop);
                    var pendingIntent       = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);
                    var notificationBuilder = new Notification.Builder(context)
                                              .SetContentTitle(waktu)
                                              .SetSmallIcon(Resource.Drawable.Logostimik)
                                              .SetContentText(matakuliah)
                                              .SetAutoCancel(true)
                                              .SetSound(soundUri)
                                              .SetContentIntent(pendingIntent)
                                              .SetPriority((int)Notification.PriorityHigh);
                    var notificationManager = NotificationManager.FromContext(context);
                    notificationManager.Notify(jadwalId, notificationBuilder.Build());
                }
                else
                {
                    intent.AddFlags(ActivityFlags.MultipleTask);
                    var pendingIntent = PendingIntent.GetActivity(context, jadwalId, intent, PendingIntentFlags.OneShot);

                    var notificationBuilder = new Android.App.Notification.Builder(context)
                                              .SetAutoCancel(true)
                                              .SetContentTitle(waktu)
                                              .SetSmallIcon(Resource.Drawable.Logostimik)
                                              .SetContentText(matakuliah)
                                              .SetAutoCancel(true)
                                              .SetSound(soundUri)
                                              .SetVibrate(new long[] { 1000, 1000, 1000 })
                                              .SetContentIntent(pendingIntent)
                                              .SetChannelId(jadwalId.ToString())
                                              .SetPriority((int)Notification.PriorityMax);


                    notificationBuilder.SetSound(Android.Net.Uri.Parse("android.resource://" + context.PackageName + "/" + Resource.Raw.atschool));//Here is FILE_NAME is the name of file that you want to play


                    var channel = new NotificationChannel(jadwalId.ToString(), matakuliah, NotificationImportance.High)
                    {
                        Description = waktu
                    };

                    var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
                    notificationManager.CreateNotificationChannel(channel);
                    MediaPlayer mp = MediaPlayer.Create(context, Resource.Raw.schoolringing);
                    if (!mp.IsPlaying)
                    {
                        mp.Start();
                    }
                    notificationManager.Notify(jadwalId, notificationBuilder.Build());
                }
            }
            catch (Exception)
            {
//                throw;
            }
        }
        public override Task Send(Notification notification)
        {
            if (notification.Id == null)
            {
                Services.Repository.CurrentScheduleId++;
                notification.Id = Services.Repository.CurrentScheduleId;
            }

            if (notification.IsScheduled)
            {
                var triggerMs = this.GetEpochMills(notification.SendTime);
                var pending   = notification.ToPendingIntent(notification.Id.Value);

                this.alarmManager.Set(
                    AlarmType.RtcWakeup,
                    Convert.ToInt64(triggerMs),
                    pending
                    );
                Services.Repository.Insert(notification);
            }
            else
            {
                var launchIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);
                launchIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                foreach (var pair in notification.Metadata)
                {
                    launchIntent.PutExtra(pair.Key, pair.Value);
                }

                var builder = new Android.App.Notification.Builder(Application.Context)
                              .SetAutoCancel(true)
                              .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))
                              .SetContentTitle(notification.Title)
                              .SetContentText(notification.Message)
                              .SetSmallIcon(AppIconResourceId)
                              .SetPriority(priority.HasValue ? priority.Value : NotificationCompat.PriorityMax)
                              .SetContentIntent(TaskStackBuilder
                                                .Create(Application.Context)
                                                .AddNextIntent(launchIntent)
                                                .GetPendingIntent(notification.Id.Value, PendingIntentFlags.OneShot)
                                                );

                if (notification.Vibrate)
                {
                    builder.SetVibrate(new long[] { 500, 500 });
                }

                if (notification.Sound != null)
                {
                    if (!notification.Sound.Contains("://"))
                    {
                        notification.Sound = $"{ContentResolver.SchemeAndroidResource}://{Application.Context.PackageName}/raw/{notification.Sound}";
                    }
                    var uri = Android.Net.Uri.Parse(notification.Sound);
                    builder.SetSound(uri);
                }
                var not = builder.Build();
                NotificationManagerCompat
                .From(Application.Context)
                .Notify(notification.Id.Value, not);
            }
            return(Task.CompletedTask);
        }