ASP.NET MVC Azure 应用程序 iOS 推送通知 | 珊瑚贝

ASP.NET MVC Azure Application iOS Push Notifications


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

我在 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 通知中心,但我不确定它能否满足我的需求。我只是在寻找可以在 Azure(ASP.NET MVC) 中托管的 C# 中的简单解决方案,我可以在其中循环一组设备令牌并推送通知。


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

注册令牌并发送通知

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

1
2
3
4
5
6
7
8
9
10
11
12
 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}”),那么消息将被传递给所有用户。详细了解标签和标签表达式以提高发送效率。

  • 嗨,谢谢,有示例代码吗?我似乎无法弄清楚如何将设备令牌与 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) 只是从集线器检索注册,但事实上,调用会对您的后端和通知集线器造成不必要的性能影响本身。


来源:https://www.codenong.com/26034758/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?