Ejemplo n.º 1
0
        public override string Send(Notification notification)
        {
            var id = NotificationSettings.Instance.CreateScheduleId();

            if (notification.IsScheduled)
            {
                var triggerMs = this.GetEpochMills(notification.SendTime);
                var pending   = notification.ToPendingIntent(id);

                this.alarmManager.Set(
                    AlarmType.RtcWakeup,
                    Convert.ToInt64(triggerMs),
                    pending
                    );

                return(id.ToString());
            }

            var launchIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);

            launchIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

            var builder = new NotificationCompat
                          .Builder(Application.Context)
                          .SetAutoCancel(true)
                          .SetContentTitle(notification.Title)
                          .SetContentText(notification.Message)
                          .SetSmallIcon(this.AppIconResourceId)
                          .SetContentIntent(TaskStackBuilder
                                            .Create(Application.Context)
                                            .AddNextIntent(launchIntent)
                                            .GetPendingIntent(id, (int)PendingIntentFlags.OneShot)
                                            );

            foreach (var pair in notification.Metadata)
            {
                builder.Extras.PutString(pair.Key, pair.Value);
            }
            if (notification.Vibrate)
            {
                builder.SetVibrate(new long[] { 500, 500 });
            }

            if (notification.Sound != null)
            {
                var uri = Android.Net.Uri.Parse(notification.Sound);
                builder.SetSound(uri);
            }
            var not = builder.Build();

            NotificationManagerCompat
            .From(Application.Context)
            .Notify(id, not);
            return(id.ToString());
        }
        public override string Send(Notification notification)
        {
            var id = this.GetNextNotificationId();

            if (notification.IsScheduled)
            {
                var pending   = notification.ToPendingIntent(id);
                var ts        = notification.SendTime.Subtract(DateTime.Now);
                var triggerMs = Convert.ToInt64(JavaSystem.CurrentTimeMillis() + ts.TotalMilliseconds);

                this.alarmManager.Set(
                    AlarmType.RtcWakeup,
                    triggerMs,
                    pending
                    );
                return(id.ToString());
            }

            var builder = new global::Android.App.Notification
                          .Builder(Application.Context)
                          .SetAutoCancel(true)
                          .SetContentTitle(notification.Title)
                          .SetContentText(notification.Message)
                          .SetSmallIcon(this.appIconId);

            if (notification.Sound != null)
            {
                var file = new File(notification.Sound);
                var uri  = Android.Net.Uri.FromFile(file);
                builder.SetSound(uri);
            }
            //if (notification.Actions != null)
            //    notification.Actions.ToList().ForEach(x => this.AddActionToBuilder(builder, x));

            var not = builder.Build();

            this.notificationManager.Notify(id, not);
            return(id.ToString());
        }
Ejemplo n.º 3
0
        public override string Send(Notification notification)
        {
            int id = notification.Id.HasValue ? notification.Id.Value : NotificationSettings.Instance.CreateScheduleId();

            if (notification.IsScheduled)
            {
                var triggerMs = notification.SendTime.ToEpochMills();
                var pending   = notification.ToPendingIntent(id);

                if (notification.Interval == NotificationInterval.None)
                {
                    this.alarmManager.Set(
                        AlarmType.RtcWakeup,
                        Convert.ToInt64(triggerMs),
                        pending
                        );
                }
                else
                {
                    //depending on the interval, calculate the next trigger
                    DateTime secondTrigger = notification.SendTime.AddDays(notification.Interval == NotificationInterval.Daily ? 1 : 7);

                    //substract the current trigger to get the inteval
                    var intervalMs = secondTrigger.ToEpochMills() - triggerMs;
                    this.alarmManager.SetRepeating(AlarmType.Rtc, triggerMs, intervalMs, pending);
                }

                return(id.ToString());
            }

            var launchIntent = Application.Context.PackageManager.GetLaunchIntentForPackage(Application.Context.PackageName);

            launchIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

            var builder = new NotificationCompat
                          .Builder(Application.Context)
                          .SetAutoCancel(true)
                          .SetContentTitle(notification.Title)
                          .SetContentText(notification.Message)
                          .SetSmallIcon(this.AppIconResourceId)
                          .SetContentIntent(TaskStackBuilder
                                            .Create(Application.Context)
                                            .AddNextIntent(launchIntent)
                                            .GetPendingIntent(id, (int)PendingIntentFlags.UpdateCurrent)
                                            );

            if (notification.Vibrate)
            {
                builder.SetVibrate(new long[] { 500, 500 });
            }

            if (notification.Sound != null)
            {
                var file = new File(notification.Sound);
                var uri  = Android.Net.Uri.FromFile(file);
                builder.SetSound(uri);
            }

            if (notification.BadgeCount.HasValue)
            {
                this.Badge = notification.BadgeCount.Value;
            }

            var not = builder.Build();

            NotificationManagerCompat
            .From(Application.Context)
            .Notify(id, not);
            return(id.ToString());
        }