在本地测试Azure托管的WebApi以向所有设备发送推送通知
如果我的问题很愚蠢,我很抱歉,但我对WebApi是新手。我遵循了一个教程,并向Azure发布了一个WebApi。我还创建了一个Xamarin移动应用程序和Azure中的所有框架,以便能够向Android和iOS发送推送通知。我已经设置了一个通知中心、应用程序服务、承载web api的web服务等。使用Azure测试,使用推送选项卡分别向ios和android发送通知,效果非常好 我发布了web api的代码。我如何在计算机上本地使用web api向两个平台(ios和android)发送通知在本地测试Azure托管的WebApi以向所有设备发送推送通知,azure,xamarin,xamarin.forms,push-notification,Azure,Xamarin,Xamarin.forms,Push Notification,如果我的问题很愚蠢,我很抱歉,但我对WebApi是新手。我遵循了一个教程,并向Azure发布了一个WebApi。我还创建了一个Xamarin移动应用程序和Azure中的所有框架,以便能够向Android和iOS发送推送通知。我已经设置了一个通知中心、应用程序服务、承载web api的web服务等。使用Azure测试,使用推送选项卡分别向ios和android发送通知,效果非常好 我发布了web api的代码。我如何在计算机上本地使用web api向两个平台(ios和android)发送通知 [R
[RoutePrefix(“sanotifications”)]
公共类SANotifController:ApiController
{
[HttpPost]
[路线(“”)
公共异步任务SendNotificationAsync([FromBody]字符串消息)
{
//获取服务器项目的设置
HttpConfiguration config=this.Configuration;
尝试
{
等待InternalSendNotificationAsync(消息,null);
}
捕获(例外情况除外)
{
//将故障结果写入日志
config.Services.GetTraceWriter().Error(例如,Message,null,“Push.SendAsync Error”);
返回请求(例如消息);
}
返回Ok();
}
[HttpPost]
[路由(“{installationid}”)]
公共异步任务SendNotificationAsync(字符串安装ID,[FromBody]字符串消息)
{
//获取服务器项目的设置
HttpConfiguration config=this.Configuration;
尝试
{
等待SendNotificationAsync(消息,安装ID);
}
捕获(例外情况除外)
{
//将故障写入日志
config.Services.GetTraceWriter().Error(例如,Message,null,“Push.SendAsync Error”);
返回请求(例如消息);
}
返回Ok();
}
异步任务InternalSendNotificationAsync(字符串消息,字符串安装ID)
{
//获取服务器项目的设置
HttpConfiguration config=this.Configuration;
//如果web api已发布到Azure以获取现有设置,请使用下面的代码
//不在本地工作
var settings=config.GetMobileAppSettingsProvider().GetMobileAppSettings();
/*
//获取移动应用程序的通知中心凭据
字符串notificationHubName=设置。notificationHubName;
字符串notificationHubConnection=settings.Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
*/
//概述页面中通知中心的名称。
//在当地工作
字符串notificationHubName=“sa1hub”;
//使用门户访问策略中的“DefaultFullSharedAccessSignature”
string notificationHubConnection=“Endpoint=sb://sahub.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=71S2@QEWF#@$";
//创建新的通知中心客户端
var hub=NotificationHubClient.CreateClientFromConnectionString(
通知连接,
通知HubName,
//不要在发布版本中使用此选项。设备数量有限。
//如果为TRUE,send方法将向设备返回一条消息
//送到。
enableTestSend:true);
//使用与这两种设备兼容的模板
//发送消息,以便所有包含“messageParam”的模板注册
//将收到通知。这包括APNS、GCM、WNS和MPNS模板注册。
var templateParams=新字典
{
[“messageParam”]=消息
};
//发送推送通知并记录结果
NotificationOutput结果=null;
if(string.IsNullOrWhiteSpace(installationId))
{
结果=Wait hub.SendTemplateNotificationAsync(templateParams).ConfigureWait(false);
}
其他的
{
结果=await hub.SendTemplateNotificationAsync(templateParams,“$InstallationId:{”+InstallationId+“}”)。ConfigureAwait(false);
}
//将成功结果写入日志。
config.Services.GetTraceWriter().Info(result.State.ToString());
返回结果;
}
}
}
为您必须与之通信的IOS设备以及我们需要的Android设备发送推送通知。Azure就像我们的应用程序和这些服务之间的中介。因此,如果您希望在没有Azure的情况下发送通知,我更愿意。它是谷歌的产品,允许我们为IOS和Android发送跨平台推送通知
我们可以在windows本地IIS中托管WebAPI,并且应该为Firebase配置
xamarin示例应用程序:为您必须与之通信的IOS设备以及我们需要的Android设备发送推送通知。Azure就像我们的应用程序和这些服务之间的中介。因此,如果您希望在没有Azure的情况下发送通知,我更愿意。它是谷歌的产品,允许我们为IOS和Android发送跨平台推送通知 我们可以在windows本地IIS中托管WebAPI,并且应该为Firebase配置
xamarin示例应用程序:在xamarin中,有两种方法可以将推送通知从服务器发送到客户端。即使是在微软论坛上,也很清楚地提到了实现这一步
[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;
}
}
}