在我的 .NET 8 MAUI 应用程序中,我想使用 Azure 通知中心添加推送通知。我已经添加了适用于 Windows 和 Android 的代码,并且无需使用外部组件即可运行。我通过调用 Azure 通知中心 API 来注册设备。
例如,对于Android,我编写了以下代码:
var firebaseToken = await FirebaseMessaging.Instance.GetToken();
var deviceInstallation = new
{
InstallationId = GetDeviceId(),
Platform = "gcm",
PushChannel = firebaseToken.ToString()
};
在本例中,
deviceInstallation
由 Firebase
提供,因为它是 Android 原生的。
我的问题是当我必须为 iOS 编写代码时。除了我猜是
apns
的平台之外,PushChannel
是什么?我在某处读到我必须在 AppDelegate
中实现此函数中的调用
[Export("application:didRegisterForRemoteNotificationsWithDeviceToken:")]
public async void RegisteredForRemoteNotifications(UIApplication application,
NSData deviceToken)
{
// ...
}
在这里,我可以使用
deviceToken
作为InstallationId
。我可以用什么来pushChannel
?
我尝试使用您的解决方案,但没有成功。但通过使用它和其他一些来源,我找到了另一个使用 Microsoft.Azure.NotificationHubs 的解决方案。
关键部分是这段代码
var hub = NotificationHubClient.CreateClientFromConnectionString(this.connectionString, this.notificationHubName);
var installation = new Installation
{
InstallationId = deviceToken,
PushChannel = deviceToken,
Platform = NotificationPlatform.Apns,
Tags = Array.Empty<string>()
};
await hub.CreateOrUpdateInstallationAsync(installation);
deviceToken 是从 RegisteredForRemoteNotifications 获取的。 Tags 是一个数组,可以通过再次调用 CreateOrUpdateInstallationAsync 来更改。