来自本地通知的iOS打开页面 [英] iOS open page from local notification

查看:210
本文介绍了来自本地通知的iOS打开页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have a local notification firing from a specified time in OS10 using the UNUserNotificationCenter. I'm trying to figure out how to open a specific page in my app when the user taps on the local notification.

任何人如何做到这一点,我对于使用C#进行iOS编程确实是新手,而且我敢肯定,这样做并不是一件罕见的事情.

Anyone how to do this I'm really new to programming for iOS in C# and I'm sure its not that uncommon of a thing to do.

推荐答案

    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.Current

获取UNUserNotificationCenter的单例实例

get the singleton instance of UNUserNotificationCenter

委托

从UNUserNotificationCenter接收事件

receive events from UNUserNotificationCenter

WillPresentNotification :被调用以向在前台运行的应用程序传递通知.如果要在前台显示通知,请参考代码,它将显示声音和警报内容.

WillPresentNotification: Called to deliver a notification to an application that is running in the foreground. If you want to show notification in the foreground, refer to the code , it will display sound and alert content.

DidReceiveNotificationResponse :在用户从应用程序的通知中选择一个操作后调用.当您点击通知并进入应用程序时,将调用此功能.然后打开一个特定页面,该页面在您的第一篇文章中.

DidReceiveNotificationResponse :Called after the user selects an action from a notification from the app. When yo tap the notification and enter the app, this function will be called. Then open a specific page which in your first post.

RequestAuthorization

RequestAuthorization

使用指定的选项请求通知授权,并处理请求的结果.所有支持通知传递的应用都需要请求授权.您的应用第一次请求授权时,会提醒用户,并给予拒绝或授予该授权的机会.

Requests notifcation authorization with the specified options, and processes the result of the request.Requesting authorization is required of all apps that support the delivery of notifications. The first time your app requests authorization, the user is alerted and given an opportunity to deny or grant that authorization.

GetNotificationSettings

GetNotificationSettings

返回应用的通知设置对象,并在返回之前对其进行处理.

Returns the notification settings object for the app, processing it before it is returned.

UNMutableNotificationContent

UNMutableNotificationContent

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

System-generated object that contains the parts of a notification, including text, sound, badge and launch images, attachments, and so on. It is shown in the notification.

UNCalendarNotificationTrigger

UNCalendarNotificationTrigger

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

Triggers the delivery of a notification at a specified day or time, either once or repeatedly. It will send the notification at 8:00 on Monday in my code.

UNNotificationRequest

UNNotificationRequest

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

Contains the content and trigger for a notification that the developer requests from UNUserNotificationCenter..

AddNotificationRequest

AddNotificationRequest

使用指定的completeHandler添加由请求指定的本地通知.

Adds the local notification that is specified by request, with the specified completionHandler.

UNNotificationAttachment(与通知一起显示的音频,视频或图像.) UNNotificationAction(可以响应通知而执行的操作.) UNNotificationCategory(执行一组包含通知类别的操作和选项.)

UNNotificationAttachment(Audio, video, or images that are displayed with notifications.) UNNotificationAction (An action that can be performed in response to a notification.) UNNotificationCategory (Implements a group of actions and options that comprise a category of notifications.)

但是我的代码可以满足您的要求. 更多信息

But your requirement can be meet with my code. More information

xamarin中的UNUserNotificationCenter

应用程序文档中的UNUserNotificationCenter

这篇关于来自本地通知的iOS打开页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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