Exemplo n.º 1
0
        public void ShowNotification(Intent intent)
        {
            var id    = intent.GetIntExtra("id", 0);
            var title = intent.GetStringExtra("title");
            var body  = intent.GetStringExtra("message");

            var main_intent = new Intent(this, typeof(MainActivity));

            main_intent.PutExtra("type", "alarm");
            main_intent.PutExtra("id", id);

            var builder = new Notification.Builder(this);

            //builder.SetGroup("keep.grass");
            builder.SetTicker(title);
            builder.SetContentTitle(title);
            builder.SetContentText(body);
            builder.SetContentIntent
            (
                PendingIntent.GetActivity
                (
                    this,
                    id,
                    main_intent,
                    PendingIntentFlags.UpdateCurrent
                )
            );
            builder.SetSmallIcon(Resource.Drawable.icon);
            builder.SetLargeIcon(BitmapFactory.DecodeResource(this.Resources, Resource.Drawable.icon));
            builder.SetDefaults(NotificationDefaults.All);
            builder.SetAutoCancel(true);

            ((NotificationManager)this.GetSystemService(NotificationService))
            .Notify(id, builder.Build());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Update the notification.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The updated notification.
        /// </returns>
        public Notification UpdateNotification(Context context)
        {
            if (builder == null)
            {
                builder = new Notification.Builder(context);
            }

            builder.SetContentTitle(this.Title);
            if (this.TotalBytes > 0 && this.CurrentBytes != -1)
            {
                builder.SetProgress((int)(this.TotalBytes >> 8), (int)(this.CurrentBytes >> 8), false);
            }
            else
            {
                builder.SetProgress(0, 0, true);
            }
            builder.SetContentText(Helpers.GetDownloadProgressString(this.CurrentBytes, this.TotalBytes));
            builder.SetContentInfo(context.GetString(Resource.String.time_remaining_notification, Helpers.GetTimeRemaining(this.TimeRemaining)));
            builder.SetSmallIcon(this.Icon != 0 ? this.Icon : Android.Resource.Drawable.StatSysDownload);
            builder.SetOngoing(true);
            builder.SetTicker(this.Ticker);
            builder.SetContentIntent(this.PendingIntent);

            return(builder.Notification);
        }
Exemplo n.º 3
0
        protected override void OnMessage(Context context, Intent intent)
        {
            Console.WriteLine("Received Notification");

            //Push Notification arrived - print out the keys/values
            if (intent != null || intent.Extras != null)
            {
                var keyset = intent.Extras.KeySet();

                foreach (var key in intent.Extras.KeySet())
                {
                    Console.WriteLine("Key: {0}, Value: {1}", key, intent.Extras.GetString(key));
                }

                var msg = intent.GetStringExtra("message");

                var n = new Notification.Builder(context);
                n.SetSmallIcon(Android.Resource.Drawable.IcDialogAlert);
                n.SetContentTitle("InFridge Expiration Notice");
                n.SetTicker(msg);
                n.SetContentText(msg);

                var nm = NotificationManager.FromContext(context);
                nm.Notify(0, n.Build());
            }
        }
        //internal static bool
        void HandleNotification(string messageBody, string messageTitle)
        {
            var intent = new Intent(Forms.Context, typeof(MainActivity));

            intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
            var pendingIntent = PendingIntent.GetActivity(Forms.Context, 0, intent, PendingIntentFlags.UpdateCurrent);


            var n = new Notification.Builder(Forms.Context);

            n.SetSmallIcon(Resource.Drawable.notification_template_icon_bg);
            n.SetLights(Android.Graphics.Color.Blue, 300, 1000);
            n.SetContentIntent(pendingIntent);
            n.SetContentTitle(messageTitle);
            n.SetTicker(messageBody);
            //n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetAutoCancel(true);
            n.SetContentText(messageBody);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(Forms.Context);

            nm.Notify(0, n.Build());

            // Make call to Xamarin.Forms here with the event handler
        }
Exemplo n.º 5
0
        /// <summary>
        /// Update the notification.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The updated notification.
        /// </returns>
        public Notification UpdateNotification(Context context)
        {
            var builder = new Notification.Builder(context);

            if (!string.IsNullOrEmpty(this.PausedText))
            {
                builder.SetContentTitle(this.PausedText);
                builder.SetContentText(string.Empty);
                builder.SetContentInfo(string.Empty);
            }
            else
            {
                builder.SetContentTitle(this.Title);
                if (this.TotalBytes > 0 && this.CurrentBytes != -1)
                {
                    builder.SetProgress((int)(this.TotalBytes >> 8), (int)(this.CurrentBytes >> 8), false);
                }
                else
                {
                    builder.SetProgress(0, 0, true);
                }

                builder.SetContentText(Helpers.GetDownloadProgressString(this.CurrentBytes, this.TotalBytes));
                builder.SetContentInfo(string.Format("{0}s left", Helpers.GetTimeRemaining(this.TimeRemaining)));
            }

            builder.SetSmallIcon(this.Icon != 0 ? this.Icon : Android.Resource.Drawable.StatSysDownload);
            builder.SetOngoing(true);
            builder.SetTicker(this.Ticker);
            builder.SetContentIntent(this.PendingIntent);

            return(builder.Notification);
        }
Exemplo n.º 6
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);


            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            txtView = (TextView)FindViewById(Resource.Id.tbNotificationInfo);
            //txtView = (TextView)FindViewById(Resource.Id.textView1);
            nReceiver = new NotificationReceiver();
            IntentFilter filter = new IntentFilter();
            filter.AddAction("com.dpwebdev.handsetfree.NOTIFICATION_LISTENER_EXAMPLE");
            RegisterReceiver(nReceiver, filter);

            // Get our button from the layout resource,
            // and attach an event to it
            Button createButton = FindViewById<Button>(Resource.Id.MyButton);
            Button catchButton = FindViewById<Button>(Resource.Id.catchNotification);

            createButton.Click += delegate
            {
                NotificationManager nManager = (NotificationManager)GetSystemService(NotificationListenerService.NotificationService);
                Notification.Builder ncomp = new Notification.Builder(this);
                ncomp.SetContentTitle("My Notification");
                ncomp.SetContentText("Notification Listener Service Example");
                ncomp.SetTicker("Notification Listener Service Example");
                ncomp.SetSmallIcon(Resource.Drawable.Icon);
                ncomp.SetAutoCancel(true);
                nManager.Notify(DateTime.Now.Millisecond, ncomp.Build());

                

            };

            catchButton.Click += delegate
            {
                
                Intent i = new Intent("com.dpwebdev.handsetfree.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
                i.PutExtra("command", "list");
                SendBroadcast(i);
            };

        }
        public void NotificateToday(string nameOfEvent)
        {
            Thread secondThread = new Thread(() =>
            {
                var nMgr                  = (NotificationManager)GetSystemService(NotificationService);
                var pendingIntent         = PendingIntent.GetActivity(this, 0, new Intent(this, typeof(DrawerAct)), 0);
                Notification.Builder noti = new Notification.Builder(this);
                var notification          = noti.SetTicker("Сегодня у Вас в планах:")
                                            .SetContentTitle("Сегодня у Вас в планах")
                                            .SetContentText(nameOfEvent)
                                            .SetSmallIcon(Resource.Drawable.service_itstime)
                                            .Build();

                nMgr.Notify(0, notification);
            }
                                             );

            secondThread.Start();
        }
        public void HandleNotification(string title, string msg)
        {
            var n = new Notification.Builder(Forms.Context);

            n.SetSmallIcon(Resource.Drawable.notification_template_icon_bg);
            n.SetLights(Android.Graphics.Color.Blue, 300, 1000);
            //n.SetContentIntent(pendingIntent);
            n.SetContentTitle(title);
            n.SetTicker(msg);
            //n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetAutoCancel(true);
            n.SetContentText(msg);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(Forms.Context);

            nm.Notify(0, n.Build());
        }
Exemplo n.º 9
0
        /// <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>
        void StartForeground()
        {
            var intent = new Intent(ApplicationContext, typeof(MainActivity));

            intent.SetAction(Intent.ActionMain);
            intent.AddCategory(Intent.CategoryLauncher);
            ExtraUtils.PutSelectedTab(intent, (int)MainActivity.TabTitle.Player);

            var pendingIntent = PendingIntent.GetActivity(
                ApplicationContext,
                0,
                intent,
                PendingIntentFlags.UpdateCurrent
                );

            var notificationBuilder = new Notification.Builder(ApplicationContext);

            notificationBuilder.SetContentTitle(CurrentEpisode.Title);
            notificationBuilder.SetContentText(
                String.Format(
                    ApplicationContext.GetString(Resource.String.NotificationContentText),
                    ApplicationContext.GetString(Resource.String.ApplicationName)
                    )
                );
            notificationBuilder.SetSmallIcon(Resource.Drawable.ic_stat_av_play_over_video);
            notificationBuilder.SetTicker(
                String.Format(
                    Application.GetString(Resource.String.NoticifationTicker),
                    CurrentEpisode.Title
                    )
                );
            notificationBuilder.SetOngoing(true);
            notificationBuilder.SetContentIntent(pendingIntent);

            StartForeground(NotificationId, notificationBuilder.Build());
        }
Exemplo n.º 10
0
        /// <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>
        void StartForeground ()
        {
            var intent = new Intent (ApplicationContext, typeof (MainActivity));
            intent.SetAction (Intent.ActionMain);
            intent.AddCategory (Intent.CategoryLauncher);
            ExtraUtils.PutSelectedTab (intent, (int) MainActivity.TabTitle.Player);

            var pendingIntent = PendingIntent.GetActivity (
                ApplicationContext,
                0,
                intent,
                PendingIntentFlags.UpdateCurrent
            );

            var notificationBuilder = new Notification.Builder (ApplicationContext);
            notificationBuilder.SetContentTitle (CurrentEpisode.Title);
            notificationBuilder.SetContentText (
                String.Format (
                    ApplicationContext.GetString (Resource.String.NotificationContentText),
                    ApplicationContext.GetString (Resource.String.ApplicationName)
                )
            );
            notificationBuilder.SetSmallIcon (Resource.Drawable.ic_stat_av_play_over_video);
            notificationBuilder.SetTicker (
                String.Format (
                    Application.GetString (Resource.String.NoticifationTicker),
                    CurrentEpisode.Title
                )
            );
            notificationBuilder.SetOngoing (true);
            notificationBuilder.SetContentIntent (pendingIntent);

            StartForeground (NotificationId, notificationBuilder.Build());
        }
Exemplo n.º 11
0
        internal static bool HandlePushNotification(Context context, Intent intent)
        {
            string message;

            if (!intent.Extras.ContainsKey("message"))
            {
                return(false);
            }

            message = intent.Extras.Get("message").ToString();
            var title = intent.Extras.Get("title").ToString();

            var activityIntent = new Intent(context, typeof(MainActivity));
            var payloadValue   = GetPayload(intent);

            activityIntent.PutExtra("payload", payloadValue);
            activityIntent.SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
            var pendingIntent = PendingIntent.GetActivity(context, 0, activityIntent, PendingIntentFlags.UpdateCurrent);

            var n = new Notification.Builder(context);

            n.SetSmallIcon(Resource.Drawable.ic_successstatus);
            n.SetLights(global::Android.Graphics.Color.Blue, 300, 1000);
            n.SetContentIntent(pendingIntent);
            n.SetContentTitle(title);
            n.SetTicker(message);
            n.SetLargeIcon(global::Android.Graphics.BitmapFactory.DecodeResource(context.Resources, Resource.Drawable.icon));
            n.SetSmallIcon(Resource.Drawable.ic_trophy_white);
            n.SetContentText(message);
            n.SetVibrate(new long[] {
                200,
                200,
                100,
            });

            var nm = NotificationManager.FromContext(context);

            nm.Notify(0, n.Build());

            if (MainActivity.IsRunning)
            {
                try
                {
                    message.ToToast();
                    var payload = GetPayload(intent);
                    var pl      = JsonConvert.DeserializeObject <NotificationPayload>(payload);

                    if (payloadValue != null)
                    {
                        Device.BeginInvokeOnMainThread(() =>
                        {
                            MessagingCenter.Send <App, NotificationPayload>(App.Current, Messages.IncomingPayloadReceivedInternal, pl);
                        });
                    }
                }
                catch (Exception e)
                {
                    InsightsManager.Report(e, Insights.Severity.Error);
                }
            }

            return(true);
        }
Exemplo n.º 12
0
        protected override void OnHandleIntent(Intent intent)
        {
            if (running)
            {
                return;
            }
            running = true;

            meetingReceiver = new MeetingReceiver();
            IntentFilter intentFilter = new IntentFilter(ActionNewMeeting);

            RegisterReceiver(meetingReceiver, intentFilter);

            Task <Core.Common.Result <IList <Meeting> > > task = eventService.FetchLatestEvent();

            while (running)
            {
                if (task.IsCompleted || task.IsCanceled || task.IsFaulted)
                {
                    if (task.IsCompleted)
                    {
                        if (!task.Result.HasError)
                        {
                            var newMeetings = task.Result.Model
                                              .Where((meeting) => { return(meeting.MeetingCreatedAt > ServiceContext.Instance.LatestMeetingTime); })
                                              .ToList();
                            if (newMeetings.Count != 0)
                            {
                                newMeetings.Sort((x, y) =>
                                {
                                    if (x.MeetingCreatedAt > y.MeetingCreatedAt)
                                    {
                                        return(-1);
                                    }
                                    else if (x.MeetingCreatedAt == y.MeetingCreatedAt)
                                    {
                                        return(0);
                                    }
                                    else
                                    {
                                        return(1);
                                    }
                                });
                                ServiceContext.Instance.LatestMeetingTime = newMeetings[0].MeetingCreatedAt;
                                StringBuilder strBuilder = new StringBuilder();
                                for (int i = 0; i < newMeetings.Count; i++)
                                {
                                    strBuilder.Append(newMeetings[i].MeetingName);
                                    if (i != newMeetings.Count - 1)
                                    {
                                        strBuilder.Append("\n");
                                    }
                                }

                                Notification.Builder builder = new Notification.Builder(this);
                                builder.SetSmallIcon(Resource.Drawable.icon);
                                builder.SetTicker($"{newMeetings.Count}条新会议\n");
                                builder.SetWhen(Java.Lang.JavaSystem.CurrentTimeMillis());
                                builder.SetContentTitle("新的会议");
                                builder.SetContentText(strBuilder.ToString());

                                Intent        intent1       = new Intent(ActionNewMeeting);
                                PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, intent1, PendingIntentFlags.CancelCurrent);
                                builder.SetContentIntent(pendingIntent);//设置点击过后跳转的activity

                                /*builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
                                 * builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
                                 * builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动*/
                                builder.SetDefaults(NotificationDefaults.All);      //设置全部

                                Notification notification = builder.Build();        //4.1以上用.build();
                                notification.Flags |= NotificationFlags.AutoCancel; // 点击通知的时候cancel掉
                                NotificationManager manager = (NotificationManager)GetSystemService(NotificationService);
                                manager.Notify(1, notification);

                                MessagingCenter.Send(Xamarin.Forms.Application.Current, "NewMeeting");
                            }
                        }
                        try
                        {
                            Thread.Sleep(3000);
                        }
                        catch (InterruptedException) {}
                    }
                    task = eventService.FetchLatestEvent();
                }
            }
        }
        /// <summary>
        /// Update the notification.
        /// </summary>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The updated notification.
        /// </returns>
        public Notification UpdateNotification(Context context)
        {
            var builder = new Notification.Builder(context);
            if (!string.IsNullOrEmpty(this.PausedText))
            {
                builder.SetContentTitle(this.PausedText);
                builder.SetContentText(string.Empty);
                builder.SetContentInfo(string.Empty);
            }
            else
            {
                builder.SetContentTitle(this.Title);
                if (this.TotalBytes > 0 && this.CurrentBytes != -1)
                {
                    builder.SetProgress((int)(this.TotalBytes >> 8), (int)(this.CurrentBytes >> 8), false);
                }
                else
                {
                    builder.SetProgress(0, 0, true);
                }

                builder.SetContentText(Helpers.GetDownloadProgressString(this.CurrentBytes, this.TotalBytes));
                builder.SetContentInfo(string.Format("{0}s left", Helpers.GetTimeRemaining(this.TimeRemaining)));
            }

            builder.SetSmallIcon(this.Icon != 0 ? this.Icon : Android.Resource.Drawable.StatSysDownload);
            builder.SetOngoing(true);
            builder.SetTicker(this.Ticker);
            builder.SetContentIntent(this.PendingIntent);

            return builder.Notification;
        }