Exemplo n.º 1
0
        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 NotificationCompat.Builder(Application.Context)
                              .SetAutoCancel(true)
                              .SetContentTitle(notification.Title)
                              .SetContentText(notification.Message)
                              .SetSmallIcon(AppIconResourceId)
                              .SetPriority(priority.HasValue ? priority.Value : NotificationCompat.PriorityDefault)
                              .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);
        }
Exemplo n.º 2
0
        public override Task Send(Notification notification)
        {
            if (notification.Id == null)
            {
                Services.Repository.CurrentScheduleId++;
                notification.Id = Services.Repository.CurrentScheduleId;
            }

            //if (string.IsNullOrEmpty(notification.IconName))
            //{
            //    notification.IconName = Notification.DefaultIcon;
            //}


            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 iconResourceId = Application.Context.Resources.GetIdentifier(notification.IconName, "drawable", Application.Context.PackageName);
                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 NotificationCompat.Builder(Application.Context)
                              .SetAutoCancel(true)
                              .SetContentTitle(notification.Title)
                              .SetContentText(notification.Message)
                              .SetSmallIcon(iconResourceId)
                              .SetContentIntent(TaskStackBuilder
                                                .Create(Application.Context)
                                                .AddNextIntent(launchIntent)
                                                .GetPendingIntent(notification.Id.Value, PendingIntentFlags.OneShot)
                                                );

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

                // Sound
                if (string.IsNullOrEmpty(notification.Sound))
                {
                    notification.Sound = Notification.DefaultSound;
                }
                if (notification.Sound != null)
                {
                    if (!notification.Sound.Contains("://"))
                    {
                        notification.Sound = $"{ContentResolver.SchemeAndroidResource}://{Application.Context.PackageName}/raw/{notification.Sound}";
                    }

                    var soundUri = DroidURI.Parse(notification.Sound);
                    builder.SetSound(soundUri);
                }
                else if (Notification.SystemSoundFallback)
                {
                    // Fallback to the system default notification sound
                    // if both Sound prop and Default prop are null
                    var soundUri = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
                    builder.SetSound(soundUri);
                }

                var not = builder.Build();
                NotificationManagerCompat
                .From(Application.Context)
                .Notify(notification.Id.Value, not);
            }
            return(Task.CompletedTask);
        }