首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于通知中心的.Net移动服务后端

问用于通知中心的.Net移动服务后端
EN

Stack Overflow用户
提问于 2014-05-19 20:42:18
回答 1查看 1.1K关注 0票数 1

我目前正在用Xamarin为安卓编写一个移动应用程序,一旦我的公司购买了Mac,我就会添加iOS,这样我就可以开始开发iOS部分了。我目前正在尝试为编写一个.Net移动服务后端,它将允许我从后端注册设备,并向特定用户和/或所有用户发送推送通知。

我一直通过开始使用通知中心跟踪Azure文档,并成功地执行了一个平台推送。然而,超越这个例子是我陷入困境的地方。在此之后的每一个例子都完全放弃了对Android的支持,只关注Windows和iOS。我看过一些有关这个主题的第九频道视频,而且都是基于Windows、Windows8和iOS的。

有没有人有一个.Net移动服务后端为将设备注册到通知集线器的后端?谢谢您抽时间见我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-20 17:06:51

我还没有GitHub上的示例代码,但下面是如何让NotificationHub在Android上工作的要点。

代码语言:javascript
复制
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操作。

代码语言:javascript
复制
[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方法从后端开始发送通知。

希望这能把你引向正确的方向。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23746859

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档