Exemplo n.º 1
0
        public void CreateWidget()
        {
            if (!initialised)
            {
                context = AndroidApp.Context;

                previousIntent            = new Intent("Previous");
                playStopIntent            = new Intent("PlayStop");
                nextIntent                = new Intent("Next");
                volumeUpIntent            = new Intent("VolumeUp");
                volumeDownIntent          = new Intent("VolumeDown");
                mediaButtonReceiverIntent = new Intent(Intent.ActionMediaButton);

                intentFilter = new IntentFilter();
                intentFilter.AddAction("Previous");
                intentFilter.AddAction("PlayStop");
                intentFilter.AddAction("Next");
                intentFilter.AddAction("VolumeUp");
                intentFilter.AddAction("VolumeDown");
                intentFilter.AddAction(Intent.ActionMediaButton);

                context.RegisterReceiver(this, intentFilter);

                CreateMediaSession();

                CreateNotificationChannel();

                initialised = true;
            }

            // Works only on Android 11
            var           resultIntent        = new Intent(context, typeof(MainActivity));
            PendingIntent notifyPendingIntent = PendingIntent.GetActivity(
                context, 0, resultIntent, PendingIntentFlags.UpdateCurrent
                );

            //

            AndroidX.Core.App.NotificationCompat.Builder builder = new AndroidX.Core.App.NotificationCompat.Builder(context, channelId);
            builder.SetVisibility(NotificationCompat.VisibilityPublic)
            .SetSmallIcon(Resource.Mipmap.icon)
            .AddAction(Resource.Drawable.ic_volume_down, "VolumeDown", PendingIntent.GetBroadcast(context, 0, volumeDownIntent, PendingIntentFlags.CancelCurrent))     // #0
            .AddAction(Resource.Drawable.ic_skip_previous, "Previous", PendingIntent.GetBroadcast(context, 1, previousIntent, PendingIntentFlags.CancelCurrent))       // #1
            .AddAction(Resource.Drawable.ic_play_circle_outline, "PlayStop", PendingIntent.GetBroadcast(context, 2, playStopIntent, PendingIntentFlags.CancelCurrent)) // #2
            .AddAction(Resource.Drawable.ic_skip_next, "Next", PendingIntent.GetBroadcast(context, 3, nextIntent, PendingIntentFlags.CancelCurrent))                   // #3
            .AddAction(Resource.Drawable.ic_volume_up, "VolumeUp", PendingIntent.GetBroadcast(context, 4, volumeUpIntent, PendingIntentFlags.CancelCurrent))           // #4
            .SetContentTitle(AppResources.UnknownTitle)
            .SetContentText(AppResources.UnknownArtist)
            .SetStyle(new AndroidX.Media.App.NotificationCompat.MediaStyle().SetShowActionsInCompactView(2 /* #2: pause button */).SetMediaSession(mediaSessionCompat.SessionToken))
            .SetOngoing(true)
            .SetSilent(true)
            .SetContentIntent(notifyPendingIntent)
            .SetVibrate(new long[] { 0L });

            notification = builder.Build();
            notificationManager.Notify(messageId, notification);
        }
Exemplo n.º 2
0
        public void UpdateWidget(string artist, string title, byte[] thumbnail)
        {
            if (notification != null && mediaSessionCompat != null)
            {
                AndroidX.Core.App.NotificationCompat.Builder builder = new AndroidX.Core.App.NotificationCompat.Builder(context, channelId);
                builder.SetVisibility(NotificationCompat.VisibilityPublic)
                .SetSmallIcon(Resource.Mipmap.icon)
                .AddAction(Resource.Drawable.ic_volume_down, "VolumeDown", PendingIntent.GetBroadcast(context, 0, volumeDownIntent, PendingIntentFlags.CancelCurrent))     // #0
                .AddAction(Resource.Drawable.ic_skip_previous, "Previous", PendingIntent.GetBroadcast(context, 1, previousIntent, PendingIntentFlags.CancelCurrent))       // #1
                .AddAction(Resource.Drawable.ic_play_circle_outline, "PlayStop", PendingIntent.GetBroadcast(context, 2, playStopIntent, PendingIntentFlags.CancelCurrent)) // #2
                .AddAction(Resource.Drawable.ic_skip_next, "Next", PendingIntent.GetBroadcast(context, 3, nextIntent, PendingIntentFlags.CancelCurrent))                   // #3
                .AddAction(Resource.Drawable.ic_volume_up, "VolumeUp", PendingIntent.GetBroadcast(context, 4, volumeUpIntent, PendingIntentFlags.CancelCurrent))           // #4
                .SetStyle(new AndroidX.Media.App.NotificationCompat.MediaStyle().SetShowActionsInCompactView(2 /* #2: pause button */).SetMediaSession(mediaSessionCompat.SessionToken))
                .SetOngoing(true)
                .SetSilent(true)
                .SetVibrate(new long[] { 0L });

                Android.Support.V4.Media.MediaMetadataCompat.Builder mediaMetadataCompat = new Android.Support.V4.Media.MediaMetadataCompat.Builder();

                if (artist.Replace("\0", "") == "")
                {
                    builder.SetContentTitle(AppResources.UnknownArtist);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, AppResources.UnknownArtist);
                }
                else
                {
                    builder.SetContentTitle(artist);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyArtist, artist);
                }

                if (title.Replace("\0", "") == "")
                {
                    builder.SetContentText(AppResources.UnknownTitle);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, AppResources.UnknownTitle);
                }
                else
                {
                    builder.SetContentText(title);
                    mediaMetadataCompat.PutString(Android.Support.V4.Media.MediaMetadataCompat.MetadataKeyTitle, title);
                }

                Bitmap bitmap = null;
                if (thumbnail != null)
                {
                    bitmap = BitmapFactory.DecodeByteArray(thumbnail, 0, thumbnail.Length);
                }
                builder.SetLargeIcon(bitmap);

                notification = builder.Build();
                notificationManager.Notify(messageId, notification);

                mediaSessionCompat.SetMetadata(mediaMetadataCompat.Build());
            }
        }