在 App.cs 类的 onResume 方法中单击通知后,如何从通知中获取值? [英] How do I get the value from the notification after clicking on the notification in the onResume method in the App.cs class?

查看:25
本文介绍了在 App.cs 类的 onResume 方法中单击通知后,如何从通知中获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 MyApp.iOS (/Services/NotifyServices.cs) 中创建通知

I am creating a notification in MyApp.iOS (/Services/NotifyServices.cs)

 var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

所有代码:

UNUserNotificationCenter.Current.GetNotificationSettings((settings) =>
            {
                var alertsAllowed = (settings.AlertSetting == UNNotificationSetting.Enabled);
                if (alertsAllowed == true)
                {
                    var content = new UNMutableNotificationContent();
                    content.Title = item.Text;
                    content.Body = item.Description;
                    content.Badge = 1;
                    

                    var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);

                    var requestID = item.Id.ToString();
                    var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

                    UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) =>
                    {
                        if (err != null)
                        {
                            // Do something with error...
                        }
                    });
                }

我想获取变量RequestId"的值;在App.cs"中在onResume"中方法.是否可以?怎么做?

I would like to get the value of the variable "RequestId" in "App.cs" in the"onResume" method. Is it possible? How to do it?

推荐答案

我们可以使用 Messaging Center 在多个项目之间传递值.

We can use Messaging Center to pass value between multiple projects.

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
         global::Xamarin.Forms.Forms.Init();
         LoadApplication(new App());
         global::Xamarin.Forms.FormsMaterial.Init();

         UNUserNotificationCenter.Current.Delegate = new MyDelegate();
         return base.FinishedLaunching(app, options);
    }

public class MyDelegate : UNUserNotificationCenterDelegate
    {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            var id = response.Notification.Request.Identifier;
            MessagingCenter.Send<object, string>(this, "Hi", id);
        }
    }

表单

在 App 构造函数中获取 id.

Forms

Get the id in App constructor.

public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage( new Page2());


        MessagingCenter.Subscribe<object, string>(this, "Hi", (obj,str) => {
                var id = str;
        });
    }

这篇关于在 App.cs 类的 onResume 方法中单击通知后,如何从通知中获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆