【问题标题】:Xamarin iOS In Azure it possible to have two notification hubs for same web API one in production and the other in development mode?Xamarin iOS 在 Azure 中是否可以为同一个 Web API 提供两个通知中心,一个处于生产模式,另一个处于开发模式?
【发布时间】:2018-11-19 21:07:51
【问题描述】:

我目前有一个移动应用程序通过 Azure 中的通知中心和 Web API 后端接收推送通知。我想为 iOS 执行应用程序测试,并将辅助通知中心设置为开发,以便注册用户不会收到来自我的测试的通知。

在 Azure 门户上,我看到后端与当前为 Apple APNS 设置为生产的通知中心相关联,如下图所示。

在移动应用程序中,在 Xamarin iOS 项目中,与后端的注册如下:

async Task SendRegistrationToServerAsync(NSData deviceToken)
        {
            //this is the template/payload used by iOS. It contains the "messageParam"
            // that will be replaced by our service

            const string templateBodyAPNS = @"{
                                                ""aps"" : {
                                                    ""alert"" : ""$(messageParam)"",
                                                    ""mutable-content"": 1
                                                },
                                            }";


            var templates = new JObject();
            templates["genericMessage"] = new JObject
            {
                {"body", templateBodyAPNS }
            };

            // send registration to web api
            // MobileServiceUrl points to the Web API mentioned above
            var client = new MobileServiceClient(MyMobileAppName.App.MobileServiceUrl); 
            await client.GetPush().RegisterAsync(deviceToken, templates);

            //get the installation id
            Console.WriteLine("Installation id1: " + client.InstallationId.ToString());
        }

问题是:

  1. Application Settings 中,我可以添加另一个 HubName 和 HubId(以分离开发和生产),并在 Connection strings 中为其添加关联的第二个连接字符串。将注册上面代码行中的 Web API 会根据应用程序运行的环境自动检测哪个集线器正在开发中,哪个集线器正在生产中?

是否会因为将两个集线器与同一个 Web api 关联而出错,并且应用程序将无法正常运行?

【问题讨论】:

    标签: azure push-notification xamarin.ios apple-push-notifications


    【解决方案1】:

    在上面的代码行中注册 Web API 是否会根据应用程序运行的环境自动检测哪个集线器正在开发中,哪个集线器正在生产中?

    如果你想将应用设置或连接字符串粘贴到开发和生产环境中,我建议你可以使用应用设置插槽设置来实现它(选中插槽设置复选框)。

    这是通过设置“粘到插槽”来实现的。我们可以从这个blog获得更多信息。

    【讨论】:

    • 感谢您花时间回答。我注意到插槽设置不是免费包含的,它用于下一级付费订阅。但我意识到,Azure 中托管的每个 Web API 都与单个通知中心相关联。所以我最终要做的是创建另一个与第一个几乎相同的 Web API,并将其与开发中心相关联。现在,当我测试时,我发布到与开发中心相关联的开发 Web api..
    【解决方案2】:

    我最终解决这个问题的方法是使用几乎相同的代码创建一个二次“开发”Web API 服务。

    我意识到每个web api 可以是连接到单个通知中心在Web API服务“概述”选项卡下的“按钮”选项卡中选择“Push”选项卡时。

    我连接到新创建的通知中心(与移动应用项目在同一命名空间内)的开发 Web API 设置为 APNS 的“沙盒”。

    我将 Xamarin 中 iOS 项目的“entitlements.plist”“apns-environemt”键更改为“development”,并将开发 p12 上传到新的通知中心。

    我还更改了 iOS 项目中向 Web API 注册的代码行,以指向创建的新 Web API(并因此向新的通知中心注册)。

    // send registration to the NEW development web api               
                var client = new MobileServiceClient(MyMobileAppName.App.NewWebAPIMobileServiceUrl); 
                await client.GetPush().RegisterAsync(deviceToken, templates);
    
                //get the installation id
                Console.WriteLine("Installation id1: " + client.InstallationId.ToString());
    

    当准备好向应用商店提交更新时,我只需将上面的代码行更改为指向原始的生产 Web api 和集线器,当然还要编辑 iOS 项目的权利 plist 和可能的通知服务扩展以将其“aps-environment”更改为“production”。

    这种方式甚至可以向开发设备的特定安装 id 发送通知,甚至可以通过新的 web api 和集线器向任何在开发模式下注册的设备发送通知。

    【讨论】:

      猜你喜欢
      • 2021-04-04
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2021-07-21
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      相关资源
      最近更新 更多