iOS 从本地通知打开页面

iOS open page from local notification

我目前在 OS10 中使用 UNUserNotificationCenter 从指定时间触发本地通知。 我正在尝试弄清楚当用户点击本地通知时如何在我的应用程序中打开特定页面。

任何人如何做到这一点我真的是 C# 中 iOS 编程的新手,我相信这并不罕见。

    UNDelegate _delegate;

    public override UIWindow Window
    {
        get;
        set;
    }

    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {

        UNUserNotificationCenter center = UNUserNotificationCenter.Current;
        _delegate = new UNDelegate();
        center.Delegate = _delegate;
        center.RequestAuthorization(UNAuthorizationOptions.Alert, (bool a, NSError error) => { });
        center.GetNotificationSettings((UNNotificationSettings setting) => {});
        registerNotification();
        return true; 
    }

    public void registerNotification()
    {

        UNMutableNotificationContent content = new UNMutableNotificationContent();
        content.Body = "body";
        content.Title = "title";
        content.Sound = UNNotificationSound.Default;

        NSDateComponents components = new NSDateComponents();
        components.Weekday = 2;
        components.Hour = 8;
        UNCalendarNotificationTrigger trigger = UNCalendarNotificationTrigger.CreateTrigger(components, true);

        UNNotificationRequest request = UNNotificationRequest.FromIdentifier("ABC", content, trigger);

        UNUserNotificationCenter.Current.AddNotificationRequest(request, (NSError error) => {
            
        });
    }


    public class UNDelegate : UNUserNotificationCenterDelegate
    {
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
        }

        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response,  Action completionHandler)
        {
            AppDelegate app = (AppDelegate)UIApplication.SharedApplication.Delegate;
            app.Window.RootViewController.PresentViewController(new ViewController1(), true, null);
        }
    }

UNUserNotificationCenter.Current

获取UNUserNotificationCenter的单例实例

Delegate

从 UNUserNotificationCenter 接收事件

WillPresentNotification:调用以向前台 运行 的应用程序发送通知。如果你想在前台显示通知,参考代码,它会显示声音和警报内容。

DidReceiveNotificationResponse :在用户从应用的通知中选择操作后调用。当您点击通知并进入应用程序时,将调用此函数。然后打开第一个 post.

中的特定页面

RequestAuthorization

请求具有指定选项的通知授权,并处理request.Requesting 支持通知传递的所有应用程序都需要授权的结果。您的应用第一次请求授权时,用户会收到警报,并有机会拒绝或授予该授权。

GetNotificationSettings

Returns 应用程序的通知设置对象,在返回之前对其进行处理。

UNMutableNotificationContent

系统生成的对象,包含通知的各个部分,包括文本、声音、徽章和启动图像、附件等。它显示在 通知。

UNCalendarNotificationTrigger

在指定日期或时间触发一次或重复发送通知。它将在我的代码中于周一 8:00 发送通知。

UNNotificationRequest

包含开发人员从 UNUserNotificationCenter 请求的通知的内容和触发器..

AddNotificationRequest

添加请求指定的本地通知,具有指定的 completionHandler。

还有很多功能,类我没有提到。例如,

UNNotificationAttachment(与通知一起显示的音频、视频或图像。) UNNotificationAction (可以响应通知执行的操作。) UNNotificationCategory (实现一组动作和选项,组成通知类别。)

但是你的要求可以用我的代码来满足。 更多信息

UNUserNotificationCenter in xamarin

UNUserNotificationCenter in app doc