Forum Xamarin Xamarin.Android

Notification configuration settings

JonAlzaJonAlza ESMember ✭✭✭

I'm developing a Xamarin.Forms app, at this moment only for Android devices. I show a notifications when a timer is over and the app is not active or the device is locked. I know that every app has it's notification configuration, and I would like to know if it is possible somehow to set default settings to enable notifications (show in lock screen, show strips...). Is it possible to configure this settings in the android manifest or something similar?

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Beginning with Android 5.0, notifications can also appear on the lock screen. The Visibility property could specify how much notification content is to be displayed when the notification appears on the lock screen.

    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) {
        builder.SetVisibility (Notification.Public);
    }
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications#lock-screen-visibility

    To disable the notification on the screen, you may need to break the screen lock. Try to use KeyguardManager and PowerManager to achieve that, and don't forget to add the required permissions.

    Similar issue on native Android you can refer to:
    https://stackoverflow.com/questions/41851654/java-android-show-notification-on-lock-screen

  • JonAlzaJonAlza ESMember ✭✭✭

    But, even with this, if I go in my Android device to Settings > Applications > My app > Notifications and disable the notifications, the strips do not appear.

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited June 4

    if I go in my Android device to Settings > Applications > My app > Notifications and disable the notifications, the strips do not appear.

    If the user disable the notification of the app manually, it'll not be able to push the notfication in code. We can not force to change the settings of the device.

    In this case, try to detect the state of notification. If it is disabled, you could request to the user to open the notification setting page to enable the option.

    NotificationManagerCompat manager = NotificationManagerCompat.From(this);
    var IsAllowed = manager.AreNotificationsEnabled();
    
    if (IsAllowed)
    {
        //notification is enabled
    }
    else
    {
        //navigate to the notification setting page to remind the user to check the option
        Intent intent = new Intent(Android.Provider.Settings.ActionAppNotificationSettings);
        intent.PutExtra("package", Application.Context.PackageName);
        intent.PutExtra("uid", Application.Context.ApplicationInfo.Uid);
    
        StartActivity(intent);
    }
    
Sign In or Register to comment.