Xamarin.iOS notifications - How to turn shouldAlwaysAlertWhileAppIsForeground to YES

Hello,

I'm using local notifications and I noticed notifications on iOS only appear when the app is in background (am i right ?).
But in my case i want the notifications even when the app is in foreground.
I saw that shouldAlwaysAlertWhileAppIsForeground settings is on "NO" so i tried to turn it to "YES" like that =>

            UNUserNotificationCenter.Current.GetNotificationSettings((settings) => {
                settings.SetValueForKey(new NSString("YES"), new NSString("shouldAlwaysAlertWhileAppIsForeground"));
            });

But it makes crash the app ...
Any tips to make it work ?
Thanks !

Tagged:

Posts

  • LandLuLandLu Member, Xamarin Team Xamurai

    on iOS 11 we can implement the IUNUserNotificationCenterDelegate to show the notification alert when our app is on foreground state. Your AppDelegate.cs can be like this:

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, 
    Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling iOS 11 foreground notification");
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }
    
    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action 
    completionHandler)
    {
        completionHandler();
    }
    

    WillPresentNotification called to deliver a notification to an application that is running in the foreground. And DidReceiveNotificationResponse will be fired when user taps the notification even though app is on the foreground.

  • LouisBeaumontLouisBeaumont Member ✭✭

    @LandLu said:
    on iOS 11 we can implement the IUNUserNotificationCenterDelegate to show the notification alert when our app is on foreground state. Your AppDelegate.cs can be like this:

    [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
    public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, 
    Action<UNNotificationPresentationOptions> completionHandler)
    {
        Console.WriteLine("Handling iOS 11 foreground notification");
        completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
    }
    
    [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
    public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action 
    completionHandler)
    {
        completionHandler();
    }
    

    WillPresentNotification called to deliver a notification to an application that is running in the foreground. And DidReceiveNotificationResponse will be fired when user taps the notification even though app is on the foreground.

    I tried this in my AppDelegate without success ...
    It is strange though because the code run without error

                UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
                {
                    if (err != null)
                    {
                        // Report error
                        System.Console.WriteLine("Error: {0}", err);
                    }
                    else
                    {
                        // Report Success
                        System.Console.WriteLine("Notification Scheduled: {0}", request);
                    }
                });
    

    Even success =>

    Notification Scheduled: <UNNotificationRequest: 0x170bbe10; identifier: not1, content: <UNNotificationContent: 0x170bbe90; title: ...
    

    But nothing in the notification center ...

  • LandLuLandLu Member, Xamarin Team Xamurai

    UNUserNotificationCenter.Current.AddNotificationRequest this method just adds a local notification to your notification center. If you want to show this notification when app is on the foreground. You should implement IUNUserNotificationCenterDelegate we talked above. To trigger these two events when notification comes, you should set the delegate when you register the notification: UNUserNotificationCenter.Current.Delegate = this; in the AppDelegate.

  • LouisBeaumontLouisBeaumont Member ✭✭

    @LandLu said:
    UNUserNotificationCenter.Current.AddNotificationRequest this method just adds a local notification to your notification center. If you want to show this notification when app is on the foreground. You should implement IUNUserNotificationCenterDelegate we talked above. To trigger these two events when notification comes, you should set the delegate when you register the notification: UNUserNotificationCenter.Current.Delegate = this; in the AppDelegate.

    Oh yeah right i forgot this

    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate, IUNUserNotificationCenterDelegate
    

    Now notifications show in foreground too !

    Thank you !

This discussion has been closed.