Я хочу попросить пользователя разрешить уведомления только тогда, когда мы запрашиваем представление «AskNotification» и когда он нажимает «Да».
Для этого я сделал следующее:
public static AppDelegate Self { get; private set; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
HtmlLabelRenderer.Initialize();
global::Xamarin.Forms.Forms.Init();
// Notifications
Firebase.Core.App.Configure();
//AllowNotifications();
...
LoadApplication(new App());
AppDelegate.Self = this;
return base.FinishedLaunching(app, options);
}
public void AllowNotifications()
{
//In iOS you must request permission to show local / remote notifications first since it is a user interrupting action.
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// Request Permissions
UNUserNotificationCenter.Current.RequestAuthorization(
UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(granted, error) =>
{
// Do something if needed
});
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
// For iOS 10 data message (sent via FCM)
Messaging.SharedInstance.Delegate = this;
}
else
{
// iOS 9 or before
var allNotificationTypes = UIUserNotificationType.Alert
| UIUserNotificationType.Badge
| UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings
.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
Console.WriteLine("-------- RegisterForRemoteNotifications");
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
И затем в моем коде Позади, когда пользователь нажимает кнопку «Разрешить» (с моей точки зрения), я делаю следующее:
AppDelegate appDelegate = AppDelegate.Self;
appDelegate.AllowNotifications();
Как видите, я используюшаблон Singleton для доступа к AppDelegate.Моя проблема заключается в том, что при вызове «AllowNotifications» внутри AppDelegate (он прокомментировал приведенный выше код) системное приглашение запрашивает пользователя и получаются уведомления.
Но когда я вызываю метод AllowNotification с другой страницыс рисунком синглтона.Появляется всплывающее окно системы, когда пользователь нажимает «Да», оно разрешает уведомление о параметрах iOS.Но я никогда не использую метод «DidReceiveMessage».
Спасибо за вашу помощь