I found delegate in Program.cs and UserNotificationCenterDelegate in UserNotificationCenterDelegate.cs:
using System;
using Foundation;
using UserNotifications;
namespace MyProjectiOS
{
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge | UNNotificationPresentationOptions.Banner | UNNotificationPresentationOptions.List);
}
}
}
Program.cs:
using System;
using Foundation;
using UIKit;
using System.Runtime.InteropServices;
using SharedCode;
using MediaManager;
using UserNotifications;
namespace MyProjectiOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
public override UIWindow? Window
{
get;
set;
}
private static Game1 game;
private MyViewController MainViewController;
internal static void RunGame()
{
int w1 = (int)(UIScreen.MainScreen.Bounds.Size.Width * UIScreen.MainScreen.Scale);
int h1 = (int)(UIScreen.MainScreen.Bounds.Size.Height * UIScreen.MainScreen.Scale);
game = new Game1(w1, h1);
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, typeof(MyProjectiOS.Program));
}
public override void FinishedLaunching(UIApplication app)
{
CrossMediaManager.Current.Init();
UIApplication.SharedApplication.IdleTimerDisabled = true;
// Request notification permissions from the user
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Alert, (approved, err) => {
// Handle approval
if (approved == false)
{
// REQUEST_P0STNOTIFICATIONS Permission Denied
}
});
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); //set delegate
UNUserNotificationCenter.Current.GetNotificationSettings((settings) =>
{
if (settings.AuthorizationStatus == UNAuthorizationStatus.NotDetermined || settings.AuthorizationStatus == UNAuthorizationStatus.Denied)
{
InvokeOnMainThread(() =>// in UI thread
{
var okAlertController = UIAlertController.Create("Title", "go to setting", UIAlertControllerStyle.Alert);
//Add Action
okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, (e) =>
{
var nurl = NSUrl.FromString(UIApplication.OpenNotificationSettingsUrl);
UIApplication.SharedApplication.OpenUrl(nurl!, new NSDictionary(), null); // This line of code will make app go to Notification page. If you test on simulator, the app will jump to Settings page.
}));
okAlertController.AddAction(UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, null));
try
{
UIApplication.SharedApplication.Delegate.GetWindow().RootViewController!.PresentViewController(okAlertController, true, null); //window.RootViewController might be null
}
catch(Exception ex)
{
}
});
}
else
{
}
});
UIApplication.SharedApplication.RegisterForRemoteNotifications();
var Window = new UIWindow(UIScreen.MainScreen.Bounds);
MainViewController = new MyViewController();
Window.RootViewController = MainViewController;
Window.MakeKeyAndVisible();
RunGame();
}
public string DeviceToken { get; set; }
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
byte[] result = new byte[deviceToken.Length];
Marshal.Copy(deviceToken.Bytes, result, 0, (int)deviceToken.Length);
DeviceToken = BitConverter.ToString(result).Replace("-", "");
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
// Has the token changed?
if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
}
// Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
var window = UIApplication.SharedApplication.KeyWindow;
if (window != null)
{
var vc = window.RootViewController;
while (vc.PresentedViewController != null)
{
vc = vc.PresentedViewController;
}
//Create Alert
var okAlertController = UIAlertController.Create("Remote Notifications", "Failed to register.", UIAlertControllerStyle.Alert);
//Add Action
okAlertController.AddAction(UIAlertAction.Create("Ok", UIAlertActionStyle.Default, null));
// Present Alert
vc.PresentViewController(okAlertController, true, null);
}
else
{
}
}
}
}
In Game1.cs I use this code:
#elif IOS
UNUserNotificationCenter.Current.GetNotificationSettings((settings) =>
{
if ((settings.AuthorizationStatus == UNAuthorizationStatus.NotDetermined) || (settings.AuthorizationStatus == UNAuthorizationStatus.Denied))
Pushnotactiveinsettings = false;
else
Pushnotactiveinsettings = true;
});
#endif
I get the following exception the first time after my application is installed on the iOS simulator. I don´t get the exception anymore after I have chosen "Allow" in the push notification dialog on the simulator. My application doesn´t crash when I click on the green arrow button to continue debugging.

at MyPojectiOS.Program.<>c.<FinishedLaunching>b__8_2() in C:\NewTest\MyProject\MyProjectiOS\Program.cs:line 72
at Foundation.NSActionDispatcher.Apply()
at UIKit.UIApplication.xamarin_UIApplicationMain(Int32 argc, IntPtr argv, IntPtr principalClassName, IntPtr delegateClassName, IntPtr* gchandle)
at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName)
at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass)
at MyProjectiOS.Program.Main(String[] args) in C:\NewTest\MyProject\MyProjectiOS\Program.cs:line 33