gpt4 book ai didi

xamarin - 我如何在 Xamarin 中使用委托(delegate)(特别是 UNUserNotificationCenterDelegate)

转载 作者:行者123 更新时间:2023-12-02 17:23:19 25 4
gpt4 key购买 nike

我需要使用 UNUserNotificationCenterDelegate 中的 iOS 10 功能.我如何在 C#/Xamarin 中实现这个委托(delegate)?

最佳答案

使用 UNUserNotificationCenterDelegate 时,请确保在 WillFinishLaunching 或应用的 UIApplicationDelegate 中的 FinishedLaunching 方法中分配它

You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching.

引用:UNUserNotificationCenterDelegate

AppDelegate.cs 示例

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) =>
{
// Handle the user approval or refusal of your notifications...
});
UNUserNotificationCenter.Current.Delegate = new MyUNUserNotificationCenterDelegate();
return true;
}

在该示例中,我正在创建/分配一个名为 MyUNUserNotificationCenterDelegate 的委托(delegate)类,因此您需要实现该类。

MyUNUserNotificationCenterDelegate 类示例:

UNUserNotificationCenterDelegate 示例将捕获发送的每个本地通知,并在锁定屏幕上显示通知或将详细信息输出到系统日志之间切换。

public class MyUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
bool toggle;
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
if (toggle)
completionHandler(UNNotificationPresentationOptions.Alert);
else
{
Console.WriteLine(notification);
completionHandler(UNNotificationPresentationOptions.None);
}
toggle = !toggle;
}
}

现在您实际上需要发送一些通知,这会设置一个简单的重复通知:

创建/安排本地通知:

// Schedule a repeating Notification...
var content = new UNMutableNotificationContent();
content.Title = new NSString("From SushiHangover");
content.Body = new NSString("StackOverflow rocks");
content.Sound = UNNotificationSound.Default;
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(timeInterval: 60, repeats: true);
var request = UNNotificationRequest.FromIdentifier(identifier: "FiveSecond", content: content, trigger: trigger);
UNUserNotificationCenter.Current.AddNotificationRequest(request, (NSError error) =>
{
if (error != null) Console.WriteLine(error);
});

每 60 秒发送一次通知,如果您在锁定屏幕上,您将每 120 秒收到一次警报...

enter image description here

推荐阅读以了解您如何使用 Xamarin.iOS/C# 与委托(delegate)、协议(protocol)和事件进行交互:

iOS uses Objective-C delegates to implement the delegation pattern, in which one object passes work off to another. The object doing the work is the delegate of the first object. An object tells its delegate to do work by sending it messages after certain things happen. Sending a message like this in Objective-C is functionally equivalent to calling a method in C#. A delegate implements methods in response to these calls, and so provides functionality to the application.

引用:Xamarin.iOS and Delegates

关于xamarin - 我如何在 Xamarin 中使用委托(delegate)(特别是 UNUserNotificationCenterDelegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com