问题
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.
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.
回答1:
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
get the singleton instance of UNUserNotificationCenter
Delegate
receive events from UNUserNotificationCenter
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 :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
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
Returns the notification settings object for the app, processing it before it is returned.
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
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
Contains the content and trigger for a notification that the developer requests from UNUserNotificationCenter..
AddNotificationRequest
Adds the local notification that is specified by request, with the specified completionHandler.
There are still many functions and Classes I don't mentioned . e.g,
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
UNUserNotificationCenter in xamarin
UNUserNotificationCenter in app doc
来源:https://stackoverflow.com/questions/45315483/ios-open-page-from-local-notification