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());
            }
        }
        // [END receive_message]

        /**
         * Create and show a simple notification containing the received FCM message.
         */
        void SendNotification(string messageBody)
        {
            var intent = new Intent(this, typeof(MainActivity));

            intent.AddFlags(ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(this, 0 /* Request code */, intent, PendingIntentFlags.OneShot);

            var defaultSoundUri     = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
            var notificationBuilder = new AndroidX.Core.App.NotificationCompat.Builder(this)
                                      .SetSmallIcon(Resource.Drawable.ic_stat_ic_notification)
                                      .SetContentTitle("FCM Message")
                                      .SetContentText(messageBody)
                                      .SetAutoCancel(true)
                                      .SetSound(defaultSoundUri)
                                      .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManager.FromContext(this);

            notificationManager.Notify(0 /* ID of notification */, notificationBuilder.Build());
        }
Exemplo n.º 4
0
        public static async void UpdateChromecastNotification(string title, string body, bool isPaused, string poster, long time, long duration)
        {
            try {
                //new Android.Support.V4.Media.Session.MediaControllerCompat.
                var builder = new AndroidX.Core.App.NotificationCompat.Builder(Application.Context);
                builder.SetContentTitle(title);
                builder.SetContentText(body);
                builder.SetAutoCancel(false);

                builder.SetSmallIcon(Resource.Drawable.round_cast_white_48dp2_4);
                builder.SetOngoing(true);

                var context = MainActivity.activity.ApplicationContext;
                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    var channelId = $"{PkgName}.general";
                    var channel   = new NotificationChannel(channelId, "General", NotificationImportance.Default);

                    NotManager.CreateNotificationChannel(channel);

                    builder.SetChannelId(channelId);
                    //https://m.media-amazon.com/images/M/MV5BMTczNTI2ODUwOF5BMl5BanBnXkFtZTcwMTU0NTIzMw@@._V1_UX182_CR0,0,182,268_AL_.jpg
                    var bitmap = await GetImageBitmapFromUrl(poster);                    //"https://m.media-amazon.com/images/M/MV5BMTczNTI2ODUwOF5BMl5BanBnXkFtZTcwMTU0NTIzMw@@._V1_UX182_CR0,0,182,268_AL_.jpg");

                    if (bitmap != null)
                    {
                        builder.SetLargeIcon(bitmap);
                    }

                    var stateBuilder = new PlaybackStateCompat.Builder()
                                       .SetActions(PlaybackStateCompat.ActionSeekTo | PlaybackStateCompat.ActionPause | PlaybackStateCompat.ActionPlay | PlaybackStateCompat.ActionStop)
                                       .SetState(
                        isPaused ? PlaybackStateCompat.StatePaused : PlaybackStateCompat.StatePlaying,
                        time, 1f, SystemClock.ElapsedRealtime()
                        );

                    mediaSession.SetPlaybackState(stateBuilder.Build());
                    mediaSession.SetFlags(MediaSessionCompat.FlagHandlesMediaButtons | MediaSessionCompat.FlagHandlesTransportControls);
                    mediaSession.SetMetadata(new Android.Support.V4.Media.MediaMetadataCompat.Builder()
                                                                                                   //.PutString(MediaMetadata.MetadataKeyArtist, "title")
                                                                                                   //.PutString(MediaMetadata.MetadataKeyTitle, "genre")
                                             .PutLong(MediaMetadata.MetadataKeyDuration, duration) //Negative duration means the duration is unknown
                                                                                                   // .PutString(MediaMetadata.MetadataKeyArt, "https://homepages.cae.wisc.edu/~ece533/images/peppers.png")
                                             .Build());
                    builder.SetColor(Android.Graphics.Color.Black.ToArgb());                       // THIS IS VERY IMPORTANT FOR THE APPERANCE OF THE SEEKBAR IN THE NOTIFICATION
                    builder.SetColorized(true);

                    mediaSession.Active = true;

                    //	mediaSession.SetMetadata(new Android.Support.V4.Media.MediaMetadataCompat() { Description = new Android.Support.V4.Media.MediaDescriptionCompat() { } })

                    List <string> actionNames = new List <string>()
                    {
                        "-30s", isPaused ? "Play" : "Pause", "+30s", "Stop"
                    };
                    List <int> sprites = new List <int>()
                    {
                        Resource.Drawable.netflixGoBack128, isPaused ? Resource.Drawable.netflixPlay128v2 : Resource.Drawable.netflixPause128v2, Resource.Drawable.netflixGoForward128, Resource.Drawable.netflixStop128v2
                    };
                    List <string> actionIntent = new List <string>()
                    {
                        "goback", isPaused ? "play" : "pause", "goforward", "stop"
                    };                                                                                                                                 // next

                    for (int i = 0; i < sprites.Count; i++)
                    {
                        var _resultIntent = new Intent(context, typeof(ChromeCastIntentService));
                        _resultIntent.PutExtra("data", actionIntent[i]);
                        var _pending = PendingIntent.GetService(context, 2337 + i,
                                                                _resultIntent,
                                                                PendingIntentFlags.UpdateCurrent
                                                                );

                        builder.AddAction(new AndroidX.Core.App.NotificationCompat.Action(sprites[i], actionNames[i], _pending));
                    }

                    builder.SetStyle(new NotificationCompat.MediaStyle().SetMediaSession(mediaSession.SessionToken).SetShowActionsInCompactView(0, 1, 2));                     // NICER IMAGE
                }

                builder.SetContentIntent(GetCurrentPending("openchrome"));

                try {
                    NotManager.Notify(CHROME_CAST_NOTIFICATION_ID, builder.Build());
                }
                catch (Exception _ex) {
                    print("EX NOTTIFY;; " + _ex);
                }
            }
            catch (Exception _ex) {
                error(_ex);
            }
        }