【问题标题】:ASP.NET MVC Azure Application iOS Push NotificationsASP.NET MVC Azure 应用程序 iOS 推送通知
【发布时间】:2014-09-25 09:14:08
【问题描述】:

我正在尝试找出处理这种情况的最佳方法。

我在 Azure 中有一个 MVC 后端,它为我的应用程序提供了一堆端点。我想添加另一个端点来处理向用户发送 iOS 推送通知。

我正在处理存储他们的设备令牌,所以我的想法是只使用 PushSharp 并遍历每个令牌并将适当的令牌推送给用户。这在本地工作得很好,但我似乎无法让 PushSharp 在 Azure 中工作。我在这里看到了这个解决方案:

https://github.com/Redth/PushSharp/wiki/Configuring-a-certificate-for-Apple-Push-Notifications-Service-on-the-Azure-platform

但是我不确定 ServiceDefinition.csdef 文件是什么以及如何将其合并到我的 MVC 解决方案中。

我已经开始研究 Azure 通知中心,但我不确定它能否满足我的需求。我只是在寻找一个简单的 C# 解决方案,它可以托管在 Azure(ASP.NET MVC) 中,我可以在其中循环一组设备令牌并推送通知。

【问题讨论】:

    标签: ios asp.net-mvc azure apple-push-notifications pushsharp


    【解决方案1】:

    如果您已经拥有 Azure 托管的 MVC 应用程序,该应用程序从最终用户 iOS 应用程序获取和存储设备令牌,那么您只需在其中创建服务总线命名空间和通知中心,上传 APNS 证书,然后您的 MVC 应用程序就可以使用Microsoft.ServiceBus.dll注册令牌并发送通知。

    这是非常基本的代码示例:

     var notificationHubClint = NotificationHubClient.CreateClientFromConnectionString("{connection string from the portal}", "{your hub name}");
    
     // Register device token with notification hub. Call it when you get device token from iOS app first time.
     var registration = await notificationHubClint.CreateAppleNativeRegistrationAsync("{device token}",new []{"SomeUserId"});
    
     // You can modify properties or refresh device token and then update registration
     registration.DeviceToken = "{new token}";
     registration.Tags.Add("{new tag}");
     await notificationHubClint.UpdateRegistrationAsync(registration);
    
     // Send notification. Call when you want to send notification to user.
     await notificationHubClint.SendAppleNativeNotificationAsync("{your message}","some user id");
    

    这里我使用标签来定位特定用户的消息。如果您只调用 SendAppleNativeNotificationAsync("{your message}"),则消息将传递给所有用户。学习more about tags and tag expressions,让您的发送更有效率。

    【讨论】:

    • 您好,谢谢,有示例代码吗?我似乎无法弄清楚如何将设备令牌与 ServiceBus DLL 一起使用
    • 确保只为单个设备调用一次 CreateAppleNativeRegistrationAsync。下次如果你想刷新令牌或添加/删除一些标签等,那么你应该调用 UpdateRegistrationAsync。
    • 这很奇怪,在更新时我认为您无法访问 Registration 对象,因为它应该只被调用一次,对吗?
    • 没关系看起来你可以启动对象来更新 var regs = new AppleRegistrationDescription("token", new[] { "userid" }); hub.UpdateRegistrationAsync(regs);
    • 您可以存储注册 ID,然后创建 AppleRegistrationDescription 类的新示例。还有一些人使用 GetRegistrationsByTagAsync("user id", 100) 或 GetRegistrationsByChannelAsync("device token", 100) 只是为了从集线器检索注册,但实际上该调用会对您的后端和通知集线器本身造成不必要的性能影响。跨度>
    猜你喜欢
    • 1970-01-01
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2018-08-15
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多