【问题标题】:.Net Mobile Service Back-End for Notification Hub用于通知中心的 .Net 移动服务后端
【发布时间】:2014-05-19 20:42:18
【问题描述】:

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

我一直遵循 Azure 文档直到 Getting Started With Notification Hub 并成功执行了单一平台推送。然而,超越这个例子是我卡住的地方。超出这一点的每个示例都完全放弃了对 Android 的支持,并且只关注 Windows Phone 和 iOS。我看过一些关于这个主题的第 9 频道视频,而且都是基于 Windows Phone、Windows 8 和 iOS 的。

是否有人提供 Azure 通知中心的 .Net 移动服务后端示例,该后端会将设备从后端注册到通知中心?感谢您的宝贵时间。

【问题讨论】:

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


    【解决方案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 方法开始从后端发送通知。

    希望这会为您指明正确的方向。

    【讨论】:

    • 你好@Kiliman,我们向注册函数发送了什么样的installationId值信息?
    • 对于其他偶然发现这一点的人来说,installationId 似乎只是一个特定于应用程序的值,仅用作标签,因此对注册没有任何关键意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    相关资源
    最近更新 更多