gpt4 book ai didi

azure - 在本地测试 Azure 托管的 WebApi 以将推送通知发送到所有设备

转载 作者:行者123 更新时间:2023-12-03 05:03:24 25 4
gpt4 key购买 nike

如果我的问题很愚蠢,我深表歉意,但我是 WebApi 的新手。我已按照教程并将 WebApi 发布到 Azure。我还创建了一个 Xamarin 移动应用程序和 Azure 中的所有框架,以便能够向 Android 和 iOS 发送推送通知。我设置了通知中心、应用程序服务、托管 Web API 的 Web 服务等。使用“推送”选项卡向 Azure 进行测试以分别向 ios 和 android 发送通知,效果非常好。

我发布了Web api的代码。如何在我的计算机上本地使用 Web api 向两个平台(ios 和 android)发送通知?

[RoutePrefix("sanotifications")]
public class SANotifController : ApiController
{


[HttpPost]
[Route("")]
public async Task<IHttpActionResult> SendNotificationAsync ([FromBody] string message)
{
//Get the settings for the server project
HttpConfiguration config = this.Configuration;

try
{
await InternalSendNotificationAsync(message, null);
}
catch (Exception ex)
{
//write the failure result to the logs
config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error");
return BadRequest(ex.Message);
}

return Ok();
}

[HttpPost]
[Route("{installationid}")]
public async Task<IHttpActionResult> SendNotificationAsync(string installationId, [FromBody] string message)
{
//get the settings for the server project
HttpConfiguration config = this.Configuration;

try
{
await SendNotificationAsync(message, installationId);
}
catch (Exception ex)
{
//write the failure to the logs
config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error");
return BadRequest(ex.Message);
}

return Ok();
}



async Task<NotificationOutcome> InternalSendNotificationAsync (string message, string installationId)
{
//Get the settings for the server project
HttpConfiguration config = this.Configuration;

//use code below if web api is already published to Azure to get existing setup
//does not work locally
var settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

/*
//Get the Notification Hubs credentials for the Mobile App
string notificationHubName = settings.NotificationHubName;
string notificationHubConnection = settings.Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
*/

//the name of the Notification Hub from the overview page.
// works locally
string notificationHubName = "sa1hub";

//use "DefaultFullSharedAccessSignature" from the portal's Access Policies
string notificationHubConnection = "Endpoint=sb://sahub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=71S2@QEWF#@$";


// create a new notification hub client
var hub = NotificationHubClient.CreateClientFromConnectionString(
notificationHubConnection,
notificationHubName,
// Don't use this in RELEASE builds. The number of devices is limited.
// If TRUE, the send method will return the devices a message was
// delivered to.
enableTestSend: true);

// use a template compatible with both devices
// send the message so that all template registrations that contain "messageParam"
// will receive the notification. This includes APNS, GCM, WNS and MPNS template registrations.
var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

// send the push notification and log the results

NotificationOutcome result = null;
if (string.IsNullOrWhiteSpace(installationId))
{
result = await hub.SendTemplateNotificationAsync(templateParams).ConfigureAwait(false);
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}

// Write the success result to the logs.
config.Services.GetTraceWriter().Info(result.State.ToString());
return result;
}


}
}

最佳答案

在 Xamarin 中,有两种方法可以将推送通知从服务器发送到客户端。即使在 Microsoft 论坛上,也非常明确地提到了实现步骤。

  1. 使用 Azure Hub 通知,我们可以在跨移动平台或 native 上轻松发送通知

Azure Push Notif信息

  • 另一个应用程序中心推送通知实现。

    Xamarin App Center Push Notification

  • 关于azure - 在本地测试 Azure 托管的 WebApi 以将推送通知发送到所有设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51508569/

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