Xamarin.iOS отправляет данные из проекта iOS в расширение службы уведомлений - PullRequest
0 голосов
/ 07 сентября 2018

В проекте Xamarin.iOS я перехватываю push-уведомление и хочу изменить его перед тем, как представить его пользователю в соответствии с некоторыми условиями, которые доступны (сохраненные настройки) в проекте iOS. Как передать данные из проекта iOS в расширение службы уведомлений и, возможно, наоборот? Или, точнее, как я могу получить доступ к пользовательским настройкам из проекта Xamarin.Forms в проекте расширения службы уведомлений iOS?

Меня больше всего интересует доступ к пользовательским настройкам, сохраненным с помощью Xamarin.Essentials

Это код шаблона для расширения службы уведомлений:

namespace NotifServiceExtension
{
    [Register("NotificationService")]
    public class NotificationService : UNNotificationServiceExtension
    {
        Action<UNNotificationContent> ContentHandler { get; set; }
        UNMutableNotificationContent BestAttemptContent { get; set; }

        protected NotificationService(IntPtr handle) : base(handle)
        {
            // Note: this .ctor should not contain any initialization logic.
        }

        public override void DidReceiveNotificationRequest(UNNotificationRequest request, Action<UNNotificationContent> contentHandler)
        {
            ContentHandler = contentHandler;
            BestAttemptContent = (UNMutableNotificationContent)request.Content.MutableCopy();

            // Modify the notification content here...
            var region = Preferences.Get("region_key", "error");   // I cannot access preferences this way   

             BestAttemptContent.Title = string.Format("{0}[modified]", BestAttemptContent.Title);          


            ContentHandler(BestAttemptContent);
        }

        public override void TimeWillExpire()
        {
            // Called just before the extension will be terminated by the system.
            // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.

            ContentHandler(BestAttemptContent);
        }
    }
}

1 Ответ

0 голосов
/ 08 сентября 2018

Самый простой способ сделать это - открыть метод класса Xamarin.Forms App.

В файле App.xaml.cs добавьте новый метод, подобный этому:

public string GetUserRegion()
{
    return Preferences.Get("region_key", "error");
}

и внутри вашего NotificationService класса вы можете получить регион следующим образом:

var region = (Xamarin.Forms.Application.Current as App)?.GetUserRegion();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...