Esempio n. 1
0
        private async void StartNotification()
        {
            if (_mediaSessionCompat != null)
            {
                Intent        intent        = new Intent(ApplicationContext, typeof(MainActivity));
                PendingIntent pendingIntent = PendingIntent.GetActivity(ApplicationContext, 0, intent, PendingIntentFlags.UpdateCurrent);
                Song          currentSong   = _queue[_pos];

                Intent audioServiceIntent = new Intent(ApplicationContext, typeof(AudioService));
                audioServiceIntent.SetAction(ActionStop);
                PendingIntent pendingCancelIntent = PendingIntent.GetService(ApplicationContext, 1, audioServiceIntent, PendingIntentFlags.CancelCurrent);

                Android.Support.V4.App.NotificationCompat.Builder builder = new Builder(ApplicationContext)
                                                                            .SetContentTitle(currentSong.Title)
                                                                            .SetContentText(currentSong.Artist)
                                                                            .SetContentInfo(currentSong.Album)
                                                                            .SetSmallIcon(Resource.Drawable.icon)
                                                                            .SetContentIntent(pendingIntent)
                                                                            .SetShowWhen(false)
                                                                            .SetOngoing(true)
                                                                            .SetVisibility(Android.Support.V4.App.NotificationCompat.VisibilityPublic)
                                                                            .SetDefaults(Android.Support.V4.App.NotificationCompat.FlagNoClear)
                                                                            .SetPriority(Android.Support.V4.App.NotificationCompat.PriorityMax);

                Bitmap artwork;
                if (!String.IsNullOrEmpty(currentSong.Artwork.ToString()))
                {
                    artwork = await BitmapFactory.DecodeFileAsync(currentSong.Artwork.ToString());
                }
                else
                {
                    artwork = await BitmapFactory.DecodeResourceAsync(ApplicationContext.Resources, Resource.Drawable.icon);
                }
                builder.SetLargeIcon(artwork);

                builder.AddAction(GenerateActionCompat(Android.Resource.Drawable.IcMediaPrevious, "Prev", ActionPrev));
                AddPlayPauseActionCompat(builder);
                builder.AddAction(GenerateActionCompat(Android.Resource.Drawable.IcMediaNext, "Next", ActionNext));

                MediaStyle style = new MediaStyle();
                style.SetShowCancelButton(true);
                style.SetCancelButtonIntent(pendingCancelIntent);
                style.SetMediaSession(_mediaSessionCompat.SessionToken);
                style.SetShowActionsInCompactView(0, 1, 2);
                builder.SetStyle(style);

                StartForeground(1, builder.Build());
            }
        }