How to show notification when clicked on play/pause button in Background Audio Streaming

AshokNagAshokNag USMember
edited November 2015 in Xamarin.Android

Hi, I created a notification based on Background Audio Streaming and added pause and play buttons to the notification using RemoteViews, when i clicked on play/pause button notification is minimizing automatically.How to show the notification without minimizing when clicked on play/pause button.

Posts

  • CasperSkouboCasperSkoubo USUniversity, Developer Group Leader

    Can you show us your code?

    You have to add a Intent and then catch it in a receiver when the button is pressed. I guess you have set the notification to open the app when you press on buttons and not fire off this intent.

  • AshokNagAshokNag USMember
    edited November 2015

    Hi @CasperSkoubo thanks for your replay. My code is like this

                 public void SetListeners(RemoteViews views)
            {
                Intent Pause=new Intent(this.parent,typeof(HelperActivity));
                Pause.PutExtra("DO", "Pause");
                PendingIntent pause = PendingIntent.GetActivity(parent, 0, Pause, 0);
                views.SetOnClickPendingIntent(Resource.Id.pause, pause);
    
                Intent Play=new Intent(this.parent,typeof(HelperActivity));
                Play.PutExtra("DO", "Play");
                PendingIntent play = PendingIntent.GetActivity(parent, 1, Play, 0);
                views.SetOnClickPendingIntent(Resource.Id.play, play);
                views.SetViewVisibility (Resource.Id.play, ViewStates.Invisible);
    
            }
    

    and my HelperActivity is

                         if (action.Equals ("Pause")) {
                            if (Constants.mediaPlayer.IsPlaying) {
                            Constants.mediaPlayer.Pause ();
    
                            var pendingIntent = PendingIntent.GetActivity (ApplicationContext, 0,
                                                    new Intent (ApplicationContext, typeof(SplashActivity)),
                                                    PendingIntentFlags.UpdateCurrent);  
                            string ns = Context.NotificationService;
                            this.cntx = cntx;
                            NotificationManager mNotificationManager; 
                            mNotificationManager = (NotificationManager)GetSystemService (ns);
                            nBuilder = new NotificationCompat.Builder (this);
    
                            Notification notification = nBuilder.Notification;
    
                            notification.SetLatestEventInfo (ApplicationContext, AppSettings.AppName, Constants.Title, pendingIntent);
                            NotificationManager notifyMgr = (NotificationManager)GetSystemService (Context.NotificationService);
                            RemoteViews contentView = new RemoteViews (AppSettings.PackageName, Resource.Layout.AudioNotification);
                            contentView.SetTextViewText (Resource.Id.title, Constants.selecteditem.Title);
                            contentView.SetTextViewText (Resource.Id.MovieTitle, AppSettings.MovieTitle);
                            notification.ContentView = contentView;
                            notification.Icon = Resource.Drawable.Icon;
                            contentView.SetViewVisibility (Resource.Id.play, ViewStates.Visible);
                            notifyMgr.Notify (1, notification);
    
                            Finish ();
                        }
    
                } else {
                            Constants.mediaPlayer.Start ();
    
                            var pendingIntent = PendingIntent.GetActivity (ApplicationContext, 0,
                                new Intent (ApplicationContext, typeof(SplashActivity)),
                                PendingIntentFlags.UpdateCurrent);  
                            string ns = Context.NotificationService;
                            this.cntx = cntx;
                            NotificationManager mNotificationManager; 
                            mNotificationManager = (NotificationManager)GetSystemService (ns);
                            nBuilder = new NotificationCompat.Builder (this);
    
                            Notification notification = nBuilder.Notification;
    
                            notification.SetLatestEventInfo (ApplicationContext, AppSettings.AppName, Constants.Title, pendingIntent);
                            NotificationManager notifyMgr = (NotificationManager)GetSystemService (Context.NotificationService);
                            RemoteViews contentView = new RemoteViews (AppSettings.PackageName, Resource.Layout.AudioNotification);
                            contentView.SetTextViewText (Resource.Id.title, Constants.selecteditem.Title);
                            contentView.SetTextViewText (Resource.Id.MovieTitle, AppSettings.MovieTitle);
                            notification.ContentView = contentView;
                            notification.Icon = Resource.Drawable.Icon;
                            contentView.SetViewVisibility (Resource.Id.play, ViewStates.Invisible);
                            notifyMgr.Notify (1, notification);
    
                            Finish ();
                } 
    
Sign In or Register to comment.