如何安排本地(无服务器)推送通知(不是警报)从我的应用程序触发?我只想从我的应用程序安排一个通知,并让它在通知中心的给定时间触发。我尝试使用 LocalNotifications,但它们似乎仅在应用程序打开时才起作用,并且仅在应用程序关闭时更新角标(Badge)。我还认为除非您使用服务器,否则无法发送推送通知。
目前,我可以安排 LocalNotification 并弹出警报,但我希望它在应用程序关闭时起作用,并且我希望在顶部弹出推送通知,而不是警报。
最佳答案
应用程序启动后,应立即请求通知权限,方法是将以下代码添加到 AppDelegate
的 FinishedLaunching
方法中,并设置所需的通知类型 ( UNAuthorizationOptions
):
...
using UserNotifications;
...
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
....
//after iOS 10
if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
UNUserNotificationCenter center = UNUserNotificationCenter.Current;
center.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.UNAuthorizationOptions.Badge, (bool arg1, NSError arg2) =>
{
});
center.Delegate = new NotificationDelegate();
}
else if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert| UIUserNotificationType.Badge| UIUserNotificationType.Sound,new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
return true;
}
iOS 10 的新功能是,当应用程序位于前台并触发通知时,应用程序可以以不同的方式处理通知。通过提供 UNUserNotificationCenterDelegate
并实现 UserNotificationCentermethod
,应用可以接管显示通知的责任。例如:
using System;
using ObjCRuntime;
using UserNotifications;
namespace workplat
{
public class NotificationDelegate:UNUserNotificationCenterDelegate
{
public NotificationDelegate()
{
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
// Do something with the notification
Console.WriteLine("Active Notification: {0}", notification);
// Tell system to display the notification anyway or use
// `None` to say we have handled the display locally.
completionHandler(UNNotificationPresentationOptions.Alert|UNNotificationPresentationOptions.Sound);
}
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
// Take action based on Action ID
switch (response.ActionIdentifier)
{
case "reply":
// Do something
break;
default:
// Take action based on identifier
if (response.IsDefaultAction)
{
// Handle default action...
}
else if (response.IsDismissAction)
{
// Handle dismiss action
}
break;
}
// Inform caller it has been handled
completionHandler();
}
}
}
要创建并向系统注册自定义操作,请使用以下代码:
public void RegisterNotification(long time)
{
UNUserNotificationCenter center = UNUserNotificationCenter.Current;
//creat a UNMutableNotificationContent which contains your notification content
UNMutableNotificationContent notificationContent = new UNMutableNotificationContent();
notificationContent.Title = "xxx";
notificationContent.Body= "xxxx";
notificationContent.Sound = UNNotificationSound.Default;
UNTimeIntervalNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(time, false);
UNNotificationRequest request = UNNotificationRequest.FromIdentifier("FiveSecond", notificationContent, trigger);
center.AddNotificationRequest(request,(NSError obj) =>
{
});
}
当你调用这个方法时,例如:
RegisterNotification(20);//set the time you want to push notification
如果您关闭应用程序,通知将在 20 秒后推送。
我的demo已经上传到我的github了,大家可以下载引用:Demo Link 。
您可以访问链接以获取更多信息和详细信息:MicroSoft Document
关于ios - Xamarin iOS 本地推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190947/