Exemple #1
0
        public static v7.NotificationCompat.Builder GenerateNotification(Context context, PushNotification push, Bundle messageIntent)
        {
            return(CoreUtility.ExecuteFunction("GenerateNotification", delegate()
            {
                if (string.IsNullOrEmpty(push.Type))
                {
                    return null;
                }

                Type activityType = typeof(SplashScreenActivity);

                Intent intent = new Intent(context, activityType);
                intent.PutExtra(BaseActivity.INIT_BUNDLE_KEY, messageIntent);
                PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.OneShot);

                string title = context.Resources.GetString(Resource.String.app_name);
                string text = push.Alert;

                string[] args = push.LocaleArgs;
                int count = 0;
                if (args != null && args.Length > 0)
                {
                    int.TryParse(args[0], out count);
                }
                Guid?route_id = null;
                if (!string.IsNullOrEmpty(push.TypeArgument))
                {
                    Guid parsed = Guid.Empty;
                    if (Guid.TryParse(push.TypeArgument, out parsed))
                    {
                        route_id = parsed;
                    }
                }
                switch (push.Type)
                {
                case "snl_sample":
                    text = string.Format(Container.StencilApp.GetLocalizedText(I18NToken.ALERT_SAMPLE, NativeAssumptions.ALERT_SAMPLE), args[0], args[1]);
                    break;

                default:
                    break;
                }
                if (string.IsNullOrEmpty(text))
                {
                    return null;
                }
                v7.NotificationCompat.Builder builder = new v7.NotificationCompat.Builder(context);
                builder
                .SetSmallIcon(Resource.Drawable.Icon)
                .SetContentTitle(title)
                .SetContentText(text)
                .SetStyle(new v7.NotificationCompat.BigTextStyle().BigText(text))
                .SetAutoCancel(true)
                .SetContentIntent(pendingIntent)
                .SetVisibility(v7.NotificationCompat.VisibilityPublic)
                .SetDefaults(v7.NotificationCompat.DefaultLights | v7.NotificationCompat.DefaultSound);

                return builder;
            }));
        }
Exemple #2
0
        public override void OnMessageReceived(RemoteMessage message)
        {
            CoreUtility.ExecuteMethod("OnMessageReceived", delegate()
            {
                // Shape like previous version of GCM for ease
                Bundle messageIntent = new Bundle();
                if (message.Data != null)
                {
                    foreach (string key in message.Data.Keys)
                    {
                        messageIntent.PutString(key, message.Data[key]);
                    }
                }

                PushNotification notification = PushNotificationProcessor.ExtractPushNotification(messageIntent);
                if (notification != null)
                {
                    notification.Alert += "!";
                    v7.NotificationCompat.Builder builder = PushNotificationProcessor.GenerateNotification(this, notification, messageIntent);
                    if (builder != null)
                    {
                        int uniqueID = (int)DateTime.UtcNow.ToUnixSeconds(); // will work until 2038
                        NotificationManager notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
                        notificationManager.Notify(uniqueID, builder.Build());
                    }
                }
            });
        }
Exemple #3
0
        void createNotification(string title, string desc)
        {
            //Create notification
            var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;

            //Create an intent to show ui
            var uiIntent = new Intent(this, typeof(MainActivity));

            //Use Notification Builder
            Android.Support.V7.App.NotificationCompat.Builder builder = new Android.Support.V7.App.NotificationCompat.Builder(this);

            //Create the notification
            //we use the pending intent, passing our ui intent over which will get called
            //when the notification is tapped.
            var notification = builder.SetContentIntent(PendingIntent.GetActivity(this, 0, uiIntent, 0))
                               .SetSmallIcon(Aliswork.Droid.Resource.Drawable.logo_icon)
                               .SetTicker(title)
                               .SetContentTitle(title)
                               .SetContentText(desc)

                               //Set the notification sound
                               .SetSound(RingtoneManager.GetDefaultUri(RingtoneType.Notification))

                               //Auto cancel will remove the notification once the user touches it
                               .SetAutoCancel(true).Build();

            //Show the notification
            notificationManager.Notify(1, notification);
        }
        void SetNotificationPlaybackState(Android.Support.V7.App.NotificationCompat.Builder builder)
        {
            var beginningOfTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            LogHelper.Debug(Tag, "updateNotificationPlaybackState. mPlaybackState=" + playbackState);
            if (playbackState == null || !started)
            {
                LogHelper.Debug(Tag, "updateNotificationPlaybackState. cancelling notification!");
                service.StopForeground(true);
                return;
            }
            if (playbackState.State == PlaybackStateCompat.StatePlaying &&
                playbackState.Position >= 0)
            {
                var timespan = ((long)(DateTime.UtcNow - beginningOfTime).TotalMilliseconds) - playbackState.Position;
                LogHelper.Debug(Tag, "updateNotificationPlaybackState. updating playback position to ", timespan / 1000, " seconds");

                builder.SetWhen(timespan).SetShowWhen(true).SetUsesChronometer(true);
            }
            else
            {
                LogHelper.Debug(Tag, "updateNotificationPlaybackState. hiding playback position");
                builder.SetWhen(0).SetShowWhen(false).SetUsesChronometer(false);
            }

            // Make sure that the notification can be dismissed by the user when we are not playing:
            builder.SetOngoing(playbackState.State == PlaybackStateCompat.StatePlaying);
        }
 void FetchBitmapFromURL(string bitmapUrl, Android.Support.V7.App.NotificationCompat.Builder builder)
 {
     AlbumArtCache.Instance.Fetch(bitmapUrl, new AlbumArtCache.FetchListener()
     {
         OnFetched = (artUrl, bitmap, icon) => {
             if (metadata != null && metadata.Description != null && metadata.Description.IconUri != null &&
                 artUrl == metadata.Description.IconUri.ToString())
             {
                 LogHelper.Debug(Tag, "fetchBitmapFromURLAsync: set bitmap to ", artUrl);
                 builder.SetLargeIcon(bitmap);
                 notificationManager.Notify(NotificationId, builder.Build());
             }
         }
     });
 }
        void AddPlayPauseAction(Android.Support.V7.App.NotificationCompat.Builder builder)
        {
            LogHelper.Debug(Tag, "updatePlayPauseAction");
            string        label;
            int           icon;
            PendingIntent intent;

            if (playbackState.State == PlaybackStateCompat.StatePlaying)
            {
                label  = "Pause";
                icon   = Resource.Drawable.ic_pause_white_24dp;
                intent = pauseIntent;
            }
            else
            {
                label  = "Play";
                icon   = Resource.Drawable.ic_play_arrow_white_24dp;
                intent = playIntent;
            }
            builder.AddAction(new Android.Support.V7.App.NotificationCompat.Action(icon, label, intent));
        }
Exemple #7
0
        private List <Paths> SearchForSongs()
        {
            string[] filters; char[] specialChars;

            using (IXmlParsable parser = new XmlFilterParser(InternalStorageHelper.InternalXmlFileLocation + "/FiltersAndSpecialChars"))
            {
                XmlFilterParser.RVal data = (XmlFilterParser.RVal)parser.FetchItems();
                filters      = data.filters;
                specialChars = data.specialChars;
            }
            List <Track> tracksFromSD = new List <Track>();
            bool         hasSd        = false;

            try
            {
                tracksFromSD = TrackFinder.GetListOfTracksFromSD(Context, filters, specialChars);
                hasSd        = true;
            }
            catch (NotMountedException e)
            {
                ISharedPreferences prefs = Context.GetSharedPreferences(Notification_Settings.Root, FileCreationMode.Private);
                if (prefs.GetBoolean(Notification_Settings.ErrorNotKey, true))
                {
                    using (var style = new Android.Support.V7.App.NotificationCompat.BigTextStyle())
                    {
                        using (Android.Support.V7.App.NotificationCompat.Builder builder = new Android.Support.V7.App.NotificationCompat.Builder(Context))
                        {
                            using (Android.Support.V4.App.NotificationCompat.Builder not = builder
                                                                                           .SetSmallIcon(Resource.Drawable.Logo).SetContentTitle("No SD card found!").SetContentText(e.Message)
                                                                                           .SetPriority((int)NotificationPriority.Min).SetStyle(style.BigText(e.Message))
                                                                                           .SetVibrate(new long[] { 1000, 1000, 1000, 1000 }))
                            {
                                var notificationManager = (NotificationManager)Context.GetSystemService(Context.NotificationService);
                                notificationManager.Notify(0, not.Build());
                            }
                        }
                    }
                }
            }
            List <Track> tracks = new List <Track>();

            try
            {
                tracks = TrackFinder.GetListOfTracksFromPhone(Context);
            }
            catch (Exception ex)
            {
                ((Activity)Context).RunOnUiThread(() => { Toast.MakeText(Context, ex.Message, ToastLength.Long).Show(); });
            }
            if (hasSd)
            {
                tracks = tracks.Union(tracksFromSD).ToList();
            }
            List <Paths> paths = new List <Paths>();

            foreach (var track in tracks)
            {
                paths.Add(new Paths()
                {
                    Path = track.Path, Title = track.FullTitle
                });
            }
            return(paths);
        }
        Notification CreateNotification()
        {
            LogHelper.Debug(Tag, "updateNotificationMetadata. mMetadata=" + metadata);
            if (metadata == null || playbackState == null)
            {
                return(null);
            }

            var notificationBuilder     = new Android.Support.V7.App.NotificationCompat.Builder(service);
            int playPauseButtonPosition = 0;

            // If skip to previous action is enabled
            if ((playbackState.Actions & PlaybackStateCompat.ActionSkipToPrevious) != 0)
            {
                notificationBuilder.AddAction(Resource.Drawable.ic_skip_previous_white_24dp,
                                              "Previous", previousIntent);

                playPauseButtonPosition = 1;
            }

            AddPlayPauseAction(notificationBuilder);

            // If skip to next action is enabled
            if ((playbackState.Actions & PlaybackStateCompat.ActionSkipToNext) != 0)
            {
                notificationBuilder.AddAction(Resource.Drawable.ic_skip_next_white_24dp,
                                              "Next", nextIntent);
            }

            MediaDescriptionCompat description = metadata.Description;

            string fetchArtUrl = null;
            Bitmap art         = null;

            if (description.IconUri != null)
            {
                String artUrl = description.IconUri.ToString();
                art = AlbumArtCache.Instance.GetBigImage(artUrl);
                if (art == null)
                {
                    fetchArtUrl = artUrl;
                    art         = BitmapFactory.DecodeResource(service.Resources,
                                                               Resource.Drawable.ic_default_art);
                }
            }

            notificationBuilder
            .SetStyle(new Android.Support.V7.App.NotificationCompat.MediaStyle()
                      .SetShowActionsInCompactView(
                          new[] { playPauseButtonPosition }) // show only play/pause in compact view
                      .SetMediaSession(sessionToken))
            .SetColor(notificationColor)
            .SetSmallIcon(Resource.Drawable.ic_notification)
            .SetVisibility(Android.Support.V7.App.NotificationCompat.VisibilityPublic)
            .SetUsesChronometer(true)
            //.SetContentIntent(CreateContentIntent())
            .SetContentTitle(description.Title)
            .SetContentText(description.Subtitle)
            .SetLargeIcon(art);

            SetNotificationPlaybackState(notificationBuilder);
            if (fetchArtUrl != null)
            {
                FetchBitmapFromURL(fetchArtUrl, notificationBuilder);
            }

            return(notificationBuilder.Build());
        }