跳到内容

MAUI Dotnet Geozones 集成

Geozones 让 Pushwoosh 能够根据设备位置发送推送通知。开始之前,请按照 MAUI Dotnet 中的说明集成基础 SDK,包括其要求。然后按照以下步骤操作:

  1. 将 Pushwoosh.DotNet.Geozones NuGet 包添加到您的解决方案中。请将其与 Pushwoosh.DotNet 一起更新。Geozones 2.0.x 与 Pushwoosh.DotNet 2.1.0 或更高版本一起使用时会在 iOS 上启动即崩溃,因此遇到这种组合时,构建会以 error PW0002 停止。
  1. Android 集成

在您的 Android 项目中,找到 MainApplication.cs 文件,并在 OnCreate() 中紧接 PushManager.Init() 之后、base.OnCreate() 之前调用 LocationManager.Init()。base.OnCreate() 会构建 MAUI 应用,如果 Init() 在此之后才运行,那么在构建期间使用 LocationManager.Instance 的页面会得到 null:

using Android.App;
using Android.Runtime;
using Pushwoosh.Android;
using PushwooshSDK.DotNet.Geozones.Android;
...
[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}
public override void OnCreate()
{
PushManager.Init();
LocationManager.Init();
base.OnCreate();
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
  1. iOS 集成

对于您的 iOS 项目,打开 AppDelegate.cs 文件并在 FinishedLaunching() 方法中调用 LocationManager.Init():

using Pushwoosh.iOS;
using PushwooshSDK.DotNet.Geozones.iOS;
using UIKit;
...
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
PushManager.Init();
LocationManager.Init();
return base.FinishedLaunching(application, launchOptions);
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
  1. 根据您应用的需求添加位置权限。

Privacy - Location When In Use Usage Description 权限应始终添加。

5. 然后通过调用 StartLocationTracking() 方法开始位置跟踪。

using PushwooshSDK.DotNet.Geozones;
...
LocationManager.Instance.StartLocationTracking();