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()); } }
/// <summary> /// Initialise the bits and bobs used for the notification /// </summary> private void InitialiseNotification() { // The mediaButtonReceiver parameter is required for pre-Lollipop SDK ComponentName mediaButtonReceiver = new(this, Java.Lang.Class.FromType(typeof(MediaButtonReceiver))); MediaSessionCompat mediaSession = new(this, AudioPlayerId, mediaButtonReceiver, null) { Active = true }; mediaStyle = new MediaStyle().SetMediaSession(mediaSession.SessionToken).SetShowActionsInCompactView(0); // The play and pause actions to be triggered when the icon is clicked playAction = new NotificationCompat.Action(Android.Resource.Drawable.IcMediaPlay, "play", PlaybackAction(PlayActionName)); pauseAction = new NotificationCompat.Action(Android.Resource.Drawable.IcMediaPause, "pause", PlaybackAction(PauseActionName)); if (Build.VERSION.SdkInt >= BuildVersionCodes.O) { NotificationChannel channel = new(AudioPlayerId, ChannelName, NotificationImportance.Low) { Description = ChannelDescription }; NotificationManager.FromContext(this).CreateNotificationChannel(channel); } }
internal static void StartNotification( Context context, MediaMetadata mediaMetadata, AndroidMedia.Session.MediaSession mediaSession, Object largeIcon, bool isPlaying) { var pendingIntent = PendingIntent.GetActivity( context, 0, new Intent(context, typeof(MainActivity)), PendingIntentFlags.UpdateCurrent); MediaMetadata currentTrack = mediaMetadata; MediaStyle style = new MediaStyle(); style.SetMediaSession(mediaSession.SessionToken); var builder = new Notification.Builder(context, CHANNEL_ID) .SetStyle(style) .SetContentTitle(currentTrack.GetString(MediaMetadata.MetadataKeyTitle)) .SetContentText(currentTrack.GetString(MediaMetadata.MetadataKeyArtist)) .SetSubText(currentTrack.GetString(MediaMetadata.MetadataKeyAlbum)) .SetSmallIcon(Resource.Drawable.player_play) .SetLargeIcon(largeIcon as Bitmap) .SetContentIntent(pendingIntent) .SetShowWhen(false) .SetOngoing(isPlaying) .SetVisibility(NotificationVisibility.Public); builder.AddAction(NotificationHelper.GenerateActionCompat(context, Drawable.IcMediaPrevious, "Previous", MediaPlayerService.ActionPrevious)); AddPlayPauseActionCompat(builder, context, isPlaying); builder.AddAction(NotificationHelper.GenerateActionCompat(context, Drawable.IcMediaNext, "Next", MediaPlayerService.ActionNext)); style.SetShowActionsInCompactView(0, 1, 2); NotificationManagerCompat.From(context).Notify(NotificationId, builder.Build()); }
/// <summary> /// When we start on the foreground we will present a notification to the user /// When they press the notification it will take them to the main page so they can control the music /// </summary> public void StartNotification(IMediaFile mediaFile, bool mediaIsPlaying, bool canBeRemoved) { var icon = _applicationContext.ApplicationInfo.Icon; _builder = new NotificationCompat.Builder(_applicationContext, CHANNEL_ID); SetMetadata(mediaFile); AddActionButtons(mediaIsPlaying); var _style = new MediaStyle(); _style.SetMediaSession(SessionToken); if (_builder.MActions.Count >= 3) { _style.SetShowActionsInCompactView(0, 1, 2); } if (_builder.MActions.Count == 2) { _style.SetShowActionsInCompactView(0, 1); } if (_builder.MActions.Count == 1) { _style.SetShowActionsInCompactView(0); } _builder.SetStyle(_style); _builder.SetSmallIcon(icon); _builder.SetContentIntent(_pendingIntent); _builder.SetOngoing(mediaIsPlaying); _builder.SetColorized(true); _builder.SetVisibility(1); //NotificationManagerCompat.From(_applicationContext) //.Notify(_notificationId, _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()) } }