通知中心的.Net移动服务后端

时间:2014-05-19 20:42:18

标签: azure xamarin.ios xamarin.android cross-platform azure-notificationhub

我目前正在使用Xamarin for Android编写移动应用程序,并且在我的公司购买Mac后将添加iOS,以便我可以开始开发iOS部分。我目前正在尝试为Azure通知中心编写.Net移动服务后端,这将允许我从后端注册设备以及向特定用户和/或所有用户发送推送通知。

我已经通过Getting Started With Notification Hub一直关注Azure文档,并成功执行了单个平台推送。然而,超越这个例子就是我陷入困境的地方。除此之外的每个示例都完全取消了Android支持,仅关注Windows Phone和iOS。我已经观看了几个关于这个主题的第9频道视频,而且它还是基于Windows Phone,Windows 8和iOS的所有视频。

有没有人有Azure Notification Hub的.Net移动服务后端示例,它会将设备从后端注册到通知中心?谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

我还没有在GitHub上提供示例代码,但这里有一个关于如何让NotificationHub在Android上工作的要点。

using Microsoft.ServiceBus.Notifications;
using Newtonsoft.Json;

public class AndroidNotificationHub
{
    private readonly NotificationHubClient _hubClient;

    public AndroidNotificationHub()
    {
        const string cn = "YourConnectionStringHere";
        const string hubPath = "YourHubPathHere";
        _hubClient = NotificationHubClient.CreateClientFromConnectionString(cn, hubPath);
    }

    public async Task<RegistrationDescription> Register(string platform, string installationId, string registrationId, string userName)
    {
        // Get registrations for the current installation ID.
        var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(installationId, 100);

        var updated = false;
        var firstRegistration = true;
        RegistrationDescription registration = null;

        // Check for existing registrations.
        foreach (var registrationDescription in regsForInstId)
        {
            if (firstRegistration)
            {
                // Update the tags.
                registrationDescription.Tags = new HashSet<string>() { installationId, userName };

                // We need to handle each platform separately.
                switch (platform)
                {
                    case "android":
                        var gcmReg = registrationDescription as GcmRegistrationDescription;
                        gcmReg.GcmRegistrationId = registrationId;
                        registration = await _hubClient.UpdateRegistrationAsync(gcmReg);
                        break;
                }
                updated = true;
                firstRegistration = false;
            }
            else
            {
                // We shouldn't have any extra registrations; delete if we do.
                await _hubClient.DeleteRegistrationAsync(registrationDescription);
            }
        }

        // Create a new registration.
        if (!updated)
        {
            switch (platform)
            {
                case "android":
                    registration = await _hubClient.CreateGcmNativeRegistrationAsync(registrationId, new[] { installationId, userName });
                    break;
            }
        }

        return registration;
    }

    // Basic implementation that sends a notification to Android clients
    public async Task<bool> SendNotification(int id, string from, string text, string tag)
    {
        try
        {
            var payload = new
            {
                data = new
                {
                    message = new
                    {
                        // these properties can be whatever you want
                        id,
                        from,
                        text,
                        when = DateTime.UtcNow.ToString("s") + "Z"
                    }
                }
            };

            var json = JsonConvert.SerializeObject(payload);

            await _hubClient.SendGcmNativeNotificationAsync(json, tag);

            return true;
        }
        catch (ArgumentException ex)
        {
            // This is expected when an APNS registration doesn't exist.
            return false;
        }
    }

    public async Task<bool> ClearRegistrations(string userName)
    {
        // Get registrations for the current installation ID.
        var regsForInstId = await _hubClient.GetRegistrationsByTagAsync(userName, 100);

        // Check for existing registrations.
        foreach (var registrationDescription in regsForInstId)
        {
            // We shouldn't have any extra registrations; delete if we do.
            await _hubClient.DeleteRegistrationAsync(registrationDescription);
        }
        return true;
    }
}

您的Android客户端需要在启动期间调用您的后端注册API。我有一个MVC行动。

[HttpPost]
public async Task<ActionResult> Register(string platform, string installationId, string registrationId, string userName)
{
    try
    {
        var hub = new AndroidNotificationHub();
        var registration = await hub.Register(platform, installationId, registrationId, userName);
        return Json(registration);
    }
    catch (Exception ex)
    {
        return Content(ex.ToString());
    }
}

移动客户端注册后,您可以通过调用SendNotification方法开始从后端发送通知。

希望这能指出你正确的方向。