Exemplo n.º 1
0
        public static bool IsNotificationEnabled(global::Android.Content.Context context)
        {
            AppOpsManager   mAppOps = (AppOpsManager)context.GetSystemService(global::Android.Content.Context.AppOpsService);
            ApplicationInfo appInfo = context.ApplicationInfo;
            String          pkg     = context.ApplicationContext.PackageName;
            int             uid     = appInfo.Uid;

            try
            {
                var appOpsClass             = Java.Lang.Class.ForName("android.app.AppOpsManager");
                var checkOpNoThrowMethod    = appOpsClass.GetMethod(CHECK_OP_NO_THROW, Java.Lang.Integer.Type, Java.Lang.Integer.Type, new Java.Lang.String().Class);
                var opPostNotificationValue = appOpsClass.GetDeclaredField(OP_POST_NOTIFICATION);
                var value = (int)opPostNotificationValue.GetInt(Java.Lang.Integer.Type);
                var mode  = (int)checkOpNoThrowMethod.Invoke(mAppOps, value, uid, pkg);
                return(mode == (int)AppOpsManagerMode.Allowed);
            }
            catch (Exception ex)
            {
                Log.Information(ex.Message);
            }
            return(false);
        }
Exemplo n.º 2
0
        public Notification.Builder CreateNotificationBuilder(global::Android.Content.Context context, 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 = 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(context, channelId);
            }
            else
#endif
            {
                builder = new Notification.Builder(context);

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

            return(builder);
        }