Пример #1
0
        /// <summary>
        /// Create Notification Channel when API >= 26.
        /// </summary>
        /// <param name="request"></param>
        public static bool CreateNotificationChannel(NotificationChannelRequest request = null)
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                return(false);
            }

            if (Application.Context.GetSystemService(Context.NotificationService) is not NotificationManager
                notificationManager)
            {
                return(false);
            }

            request ??= new NotificationChannelRequest();

            if (string.IsNullOrWhiteSpace(request.Name))
            {
                request.Name = "General";
            }

            if (string.IsNullOrWhiteSpace(request.Id))
            {
                request.Id = DefaultChannelId;
            }

            // you can't change the importance or other notification behaviors after this.
            // once you create the channel, you cannot change these settings and
            // the user has final control of whether these behaviors are active.
            using var channel = new NotificationChannel(request.Id, request.Name, request.Importance)
                  {
                      Description          = request.Description,
                      Group                = request.Group,
                      LightColor           = request.LightColor,
                      LockscreenVisibility = request.LockScreenVisibility,
                  };
            var soundUri = GetSoundUri(request.Sound);

            if (soundUri != null)
            {
                using var audioAttributesBuilder = new AudioAttributes.Builder();
                var audioAttributes = audioAttributesBuilder.SetUsage(AudioUsageKind.Notification)
                                      ?.SetContentType(AudioContentType.Music)
                                      ?.Build();
                channel.SetSound(soundUri, audioAttributes);
            }

            if (request.VibrationPattern != null)
            {
                channel.SetVibrationPattern(request.VibrationPattern);
            }

            channel.SetShowBadge(request.ShowBadge);
            channel.EnableLights(request.EnableLights);
            channel.EnableVibration(request.EnableVibration);
            channel.SetBypassDnd(request.CanBypassDnd);

            notificationManager.CreateNotificationChannel(channel);

            return(true);
        }
Пример #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            //Forms.SetFlags(new[]
            //{
            //    "CarouselView_Experimental",
            //    "IndicatorView_Experimental",
            //    "SwipeView_Experimental",
            //    "CollectionView_Experimental"
            //});

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            FFImageLoading.Forms.Platform.CachedImageRenderer.Init(true);

            var channel = new NotificationChannelRequest();

            channel.LightColor = Android.Graphics.Color.Green;
            long[] vibrationPattern = { 100, 200, 300, 400, 500, 400, 300, 200, 400 };
            channel.VibrationPattern     = vibrationPattern;
            channel.LockscreenVisibility = NotificationVisibility.Public;

            // Must create a Notification Channel when API >= 26
            NotificationCenter.CreateNotificationChannel(channel);
            // you can created multiple Notification Channels with different names.
            NotificationCenter.CreateNotificationChannel();

            //For apps that target Android 5.1(API level 22) or lower, there is nothing more that needs to be done.
            //Apps that will run on Android 6.0(API 23 level 23) or higher should ask Run time permission checks.
            //Handles this Exception: Xamarin: Android: System.UnauthorizedAccessException: Access to the path is denied
            if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
            {
                if (!(CheckPermissionGranted(Manifest.Permission.ReadExternalStorage) && !CheckPermissionGranted(Manifest.Permission.WriteExternalStorage)))
                {
                    RequestPermission();
                }
            }

            LoadApplication(new App());

            NotificationCenter.NotifyNotificationTapped(base.Intent);
        }