Xamarin IOS-关闭应用程序时显示本地通知

时间:2020-07-23 07:49:25

标签: xamarin.ios geolocation localnotification xamarin-forms-4

我有一个Xamarin IOS应用程序,即使该应用程序被杀死,它也会每10秒获取一次用户位置。我使用以下库:“ https://jamesmontemagno.github.io/GeolocatorPlugin/”。

我想要的是:当应用关闭或打开并且用户位于特定位置时,我想显示本地通知。关闭应用程序是否有可能?我找不到关于此的信息,因为它总是与远程通知有关。

1 个答案:

答案 0 :(得分:1)

应用程序启动后,应立即通过以下方式请求通知权限:将以下代码添加到FinishedLaunching的{​​{1}}方法中并设置所需的通知类型(AppDelegate):

UNAuthorizationOptions

iOS 10的新增功能,当应用程序位于前台并触发通知时,它可以以不同方式处理通知。通过提供... 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; } 并实现 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) => 
        {
           


        });

    }

如果您关闭应用,则会在20秒后推送通知。您可以在上传位置后放入此行。

我已将演示文件上传到我的github,您可以下载它作为参考:Demo Link

您可以访问该链接以获取更多信息和详细信息:MicroSoft Document