Beispiel #1
0
        public override Task IssueNotificationAsync(string title, string message, string id, bool alertUser, Protocol protocol, int?badgeNumber, NotificationUserResponseAction userResponseAction, string userResponseMessage)
        {
            if (_notificationManager == null)
            {
                return(Task.CompletedTask);
            }
            else if (message == null)
            {
                CancelNotification(id);
            }
            else
            {
                Intent notificationIntent = new Intent(Application.Context, typeof(AndroidMainActivity));
                notificationIntent.PutExtra(NOTIFICATION_USER_RESPONSE_ACTION_KEY, userResponseAction.ToString());
                notificationIntent.PutExtra(NOTIFICATION_USER_RESPONSE_MESSAGE_KEY, userResponseMessage);

                PendingIntent notificationPendingIntent = PendingIntent.GetActivity(Application.Context, 0, notificationIntent, PendingIntentFlags.OneShot);

                SensusNotificationChannel notificationChannel = SensusNotificationChannel.Default;

                if (userResponseAction == NotificationUserResponseAction.DisplayPendingSurveys)
                {
                    notificationChannel = SensusNotificationChannel.Survey;
                }

                // reset channel to silent if we're not notifying/alerting or if we're in an exclusion window
                if (!alertUser || (protocol != null && protocol.TimeIsWithinAlertExclusionWindow(DateTime.Now.TimeOfDay)))
                {
                    notificationChannel = SensusNotificationChannel.Silent;
                }

                Notification.Builder notificationBuilder = CreateNotificationBuilder(notificationChannel)
                                                           .SetContentTitle(title)
                                                           .SetContentText(message)
                                                           .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                           .SetContentIntent(notificationPendingIntent)
                                                           .SetAutoCancel(true)
                                                           .SetOngoing(false);
                if (badgeNumber != null)
                {
                    notificationBuilder.SetNumber(badgeNumber.Value);
                }

                // use big-text style for long messages
                if (message.Length > 20)
                {
                    Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle();
                    bigTextStyle.BigText(message);
                    notificationBuilder.SetStyle(bigTextStyle);
                }

                _notificationManager.Notify(id, 0, notificationBuilder.Build());
            }

            return(Task.CompletedTask);
        }
Beispiel #2
0
 private string GetChannelDescription(SensusNotificationChannel channel)
 {
     if (channel == SensusNotificationChannel.ForegroundService)
     {
         return("Notifications about Sensus services that are running in the background");
     }
     else if (channel == SensusNotificationChannel.Survey)
     {
         return("Notifications about Sensus surveys you can take");
     }
     else
     {
         return("General Sensus notifications");
     }
 }
Beispiel #3
0
 private string GetChannelName(SensusNotificationChannel channel)
 {
     if (channel == SensusNotificationChannel.ForegroundService)
     {
         return("Background Services");
     }
     else if (channel == SensusNotificationChannel.Survey)
     {
         return("Surveys");
     }
     else
     {
         return("Notifications");
     }
 }
Beispiel #4
0
        /// <summary>
        /// Issues the notification.
        /// </summary>
        /// <param name="title">Title.</param>
        /// <param name="message">Message.</param>
        /// <param name="id">Identifier of notification.</param>
        /// <param name="protocolId">Protocol identifier to check for alert exclusion time windows.</param>
        /// <param name="alertUser">If set to <c>true</c> alert user.</param>
        /// <param name="displayPage">Display page.</param>
        public override void IssueNotificationAsync(string title, string message, string id, string protocolId, bool alertUser, DisplayPage displayPage)
        {
            if (_notificationManager == null)
            {
                return;
            }

            Task.Run(() =>
            {
                if (message == null)
                {
                    CancelNotification(id);
                }
                else
                {
                    Intent notificationIntent = new Intent(_service, typeof(AndroidSensusService));
                    notificationIntent.PutExtra(DISPLAY_PAGE_KEY, displayPage.ToString());
                    PendingIntent notificationPendingIntent = PendingIntent.GetService(_service, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);

                    SensusNotificationChannel notificationChannel = SensusNotificationChannel.Default;

                    if (displayPage == DisplayPage.PendingSurveys)
                    {
                        notificationChannel = SensusNotificationChannel.Survey;
                    }

                    // reset channel to silent if we're not alerting or if we're in an exclusion window
                    if (!alertUser || Protocol.TimeIsWithinAlertExclusionWindow(protocolId, DateTime.Now.TimeOfDay))
                    {
                        notificationChannel = SensusNotificationChannel.Silent;
                    }

                    Notification.Builder notificationBuilder = CreateNotificationBuilder(_service, notificationChannel)
                                                               .SetContentTitle(title)
                                                               .SetContentText(message)
                                                               .SetSmallIcon(Resource.Drawable.ic_launcher)
                                                               .SetContentIntent(notificationPendingIntent)
                                                               .SetAutoCancel(true)
                                                               .SetOngoing(false);

                    _notificationManager.Notify(id, 0, notificationBuilder.Build());
                }
            });
        }
Beispiel #5
0
 private NotificationImportance GetChannelImportance(SensusNotificationChannel channel)
 {
     if (channel == SensusNotificationChannel.ForegroundService)
     {
         return(NotificationImportance.Min);
     }
     else if (channel == SensusNotificationChannel.Survey)
     {
         return(NotificationImportance.Max);
     }
     else if (channel == SensusNotificationChannel.Silent)
     {
         return(NotificationImportance.Min);
     }
     else
     {
         return(NotificationImportance.Default);
     }
 }
Beispiel #6
0
 private bool GetChannelSilent(SensusNotificationChannel channel)
 {
     if (channel == SensusNotificationChannel.Default)
     {
         return(false);
     }
     else if (channel == SensusNotificationChannel.ForegroundService)
     {
         return(true);
     }
     else if (channel == SensusNotificationChannel.Silent)
     {
         return(true);
     }
     else if (channel == SensusNotificationChannel.Survey)
     {
         return(false);
     }
     else
     {
         return(false);
     }
 }
Beispiel #7
0
        public Notification.Builder CreateNotificationBuilder(SensusNotificationChannel channel)
        {
            global::Android.Net.Uri notificationSoundURI = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

            AudioAttributes notificationAudioAttributes = new AudioAttributes.Builder()
                                                          .SetContentType(AudioContentType.Unknown)
                                                          .SetUsage(AudioUsageKind.NotificationEvent).Build();

            long[] vibrationPattern = { 0, 250, 50, 250 };

            bool silent = GetChannelSilent(channel);

            Notification.Builder builder;

            // see the Backwards Compatibility article for more information
#if __ANDROID_26__
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                NotificationManager notificationManager = Application.Context.GetSystemService(global::Android.Content.Context.NotificationService) as NotificationManager;

                string channelId = channel.ToString();

                if (notificationManager.GetNotificationChannel(channelId) == null)
                {
                    NotificationChannel notificationChannel = new NotificationChannel(channelId, GetChannelName(channel), GetChannelImportance(channel))
                    {
                        Description = GetChannelDescription(channel)
                    };

                    if (silent)
                    {
                        notificationChannel.SetSound(null, null);
                        notificationChannel.EnableVibration(false);
                    }
                    else
                    {
                        notificationChannel.SetSound(notificationSoundURI, notificationAudioAttributes);
                        notificationChannel.EnableVibration(true);
                        notificationChannel.SetVibrationPattern(vibrationPattern);
                    }

                    notificationManager.CreateNotificationChannel(notificationChannel);
                }

                builder = new Notification.Builder(Application.Context, channelId);
            }
            else
#endif
            {
#pragma warning disable 618
                builder = new Notification.Builder(Application.Context);

                if (silent)
                {
                    builder.SetSound(null);
                    builder.SetVibrate(null);
                }
                else
                {
                    builder.SetSound(notificationSoundURI, notificationAudioAttributes);
                    builder.SetVibrate(vibrationPattern);
                }
#pragma warning restore 618
            }

            return(builder);
        }
Beispiel #8
0
 private bool GetChannelSilent(SensusNotificationChannel channel)
 {
     return(channel != SensusNotificationChannel.Survey);
 }