简体   繁体   English

来自Azure通知中心的Xamarin.Forms PushNotifications

[英]Xamarin.Forms PushNotifications from Azure Notification Hub

I'm trying to receive push notifications from Azure's Notification Hub on Xamarin.Forms. 我正在尝试从Xamarin.Forms上Azure的通知中心接收推送通知。 I tried Azure's example for Android. 我尝试了适用于Android的Azure的示例。 The problem is, Azure's tutorial connects to the database which sends the notifications when database is changed. 问题是, Azure的教程连接到数据库,该数据库在数据库更改时发送通知。 I don't want that. 我不要 There's already a CMS in place that triggers the push notifications. 已经有一个CMS可以触发推送通知。

So how do I connect the app just to the Notification Hub and listen for the notifications? 那么,如何将应用程序仅连接到通知中心并收听通知?

Found the solution. 找到了解决方案。 I had to register with Azure notification hub too. 我也必须向Azure通知中心注册。 If someone is stuck, follow this: 如果有人被卡住,请遵循以下步骤:

https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-notification-hubs-android-get-started/ https://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-notification-hubs-android-get-started/

Or, follow the example given in Xamarin's "Azure Messaging" component here: https://components.xamarin.com/gettingstarted/azure-messaging 或者,按照此处Xamarin的“ Azure消息传递”组件中给出的示例进行操作: https : //components.xamarin.com/gettingstarted/azure-messaging

I am actually doing this right this very second. 我实际上是第二秒钟就这样做了。 Not with Xamarin but with a Windows Phone app. 不是使用Xamarin,而是使用Windows Phone应用程序。 Hopefully this can help. 希望这会有所帮助。

  1. Make sure you register your app with the dev portal and create a WNS association. 确保您在开发门户网站注册了您的应用程序并创建了WNS关联。
  2. Once that is done you can go back into your mobile service or notification hub and add the package ssid and the client secret that you just got from the dev portal. 完成后,您可以返回到移动服务或通知中心,并添加软件包ssid和刚刚从开发门户获得的客户端密码。
  3. Add code to associate your app with the notification hub 添加代码以将您的应用与通知中心相关联

    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); var channel =等待PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

    var hub = new NotificationHub("", ""); var hub = new NotificationHub(“”,“”); var result = await hub.RegisterNativeAsync(channel.Uri); var result = await hub.RegisterNativeAsync(channel.Uri);

That can go in the OnLaunched event in the App.xaml.cs. 这可以在App.xaml.cs中的OnLaunched事件中进行。 This will register the app with the hub. 这会将应用程序注册到中心。 Make sure you choose the type of notification the app will show in the package manifest (toast..etc..) 确保选择应用程序将在程序包清单中显示的通知类型(toast..etc ..)

  1. In order to execute the notification it sounds like you might want a custom API to call the notification hub. 为了执行通知,听起来您可能希望自定义API调用通知中心。 That is what I am doing. 那就是我在做什么。 When a particular action happens I call the custom API and it will issue the push. 当发生特定操作时,我将调用自定义API,它将发出推送。

This is written up in detail at the following url https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-started/ 这是在以下URL https://azure.microsoft.com/zh-cn/documentation/articles/notification-hubs-windows-store-dotnet-get-started/中详细编写的

There are some other good docs if you don't want to notify everyone, such as notify just subscribers of a category. 如果您不想通知所有人,例如还有只通知某个类别的订阅者,那么还有其他一些不错的文档。 Hope this helps. 希望这可以帮助。 It might not be 100% on point with Xamarin but should be close. Xamarin可能不是100%正确,但应该接近。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM