private async void StartNotification() { if (_mediaSessionCompat != null) { Intent intent = new Intent(ApplicationContext, typeof(MainActivity)); PendingIntent pendingIntent = PendingIntent.GetActivity(ApplicationContext, 0, intent, PendingIntentFlags.UpdateCurrent); Track currentSong = _queue[_pos]; Intent audioServiceIntent = new Intent(ApplicationContext, typeof(AudioService)); audioServiceIntent.SetAction(ActionStop); PendingIntent pendingCancelIntent = PendingIntent.GetService(ApplicationContext, 1, audioServiceIntent, PendingIntentFlags.CancelCurrent); NotificationCompat.Builder builder = new NotificationCompat.Builder(ApplicationContext) .SetContentTitle(currentSong.Title) .SetContentText(currentSong.Artist) .SetContentInfo(currentSong.Album) .SetSmallIcon(Resource.Drawable.playlist_icon) .SetContentIntent(pendingIntent) .SetShowWhen(false) .SetOngoing(true) .SetVisibility(NotificationCompat.VisibilityPublic) .SetDefaults(NotificationCompat.FlagNoClear) .SetPriority(NotificationCompat.PriorityMax); Bitmap artwork; if (!String.IsNullOrEmpty(currentSong.Image.ToString())) { artwork = await BitmapFactory.DecodeFileAsync(currentSong.Image.ToString()); } else { artwork = await BitmapFactory.DecodeResourceAsync(ApplicationContext.Resources, Resource.Drawable.playlist_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()); } }
private async void StartNotification(bool autoclose) { 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); var builder = new NotificationCompat.Builder(this, CHANNEL_ID); if (autoclose) { builder = new NotificationCompat.Builder(this, CHANNEL_ID) .SetContentTitle(currentSong.Title) .SetContentText(currentSong.Artist) .SetContentInfo(currentSong.Album) .SetSmallIcon(Resource.Drawable.icon) .SetContentIntent(pendingIntent) .SetOngoing(false) .SetVisibility(Android.Support.V4.App.NotificationCompat.VisibilityPublic) .SetDefaults(Android.Support.V4.App.NotificationCompat.FlagAutoCancel) .SetPriority(Android.Support.V4.App.NotificationCompat.PriorityMax); } else { builder = new NotificationCompat.Builder(this, CHANNEL_ID) .SetContentTitle(currentSong.Title) .SetContentText(currentSong.Artist) .SetContentInfo(currentSong.Album) .SetSmallIcon(Resource.Drawable.icon) .SetContentIntent(pendingIntent) .SetOngoing(true) .SetVisibility(Android.Support.V4.App.NotificationCompat.VisibilityPublic) .SetDefaults(Android.Support.V4.App.NotificationCompat.FlagAutoCancel) .SetPriority(Android.Support.V4.App.NotificationCompat.PriorityMax); } Bitmap artwork; //if (currentSong.Artwork !=null) //{ // artwork = await BitmapFactory.DecodeByteArrayAsync(currentSong.Artwork, 0, currentSong.Artwork.Length); //} //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); if (Build.VERSION.SdkInt < BuildVersionCodes.O) { // Notification channels are new in API 26 (and not a part of the // support library). There is no need to create a notification // channel on older versions of Android. return; } var channel = new NotificationChannel(CHANNEL_ID, "JukeBox Notifications", NotificationImportance.Low) { Description = "JukeBox appear in this channel" }; var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.CreateNotificationChannel(channel); notificationManager.Notify(1, builder.Build()); // StartForeground(1, builder.Build()) } }