简体   繁体   中英

Xamarin.Forms PushNotifications from Azure Notification Hub

I'm trying to receive push notifications from Azure's Notification Hub on Xamarin.Forms. I tried Azure's example for Android. The problem is, Azure's tutorial connects to the database which sends the notifications when database is changed. I don't want that. There's already a CMS in place that triggers the push notifications.

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. If someone is stuck, follow this:

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

I am actually doing this right this very second. Not with Xamarin but with a Windows Phone app. Hopefully this can help.

  1. Make sure you register your app with the dev portal and create a WNS association.
  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.
  3. Add code to associate your app with the notification hub

    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

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

That can go in the OnLaunched event in the App.xaml.cs. 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..)

  1. In order to execute the notification it sounds like you might want a custom API to call the notification hub. That is what I am doing. When a particular action happens I call the custom API and it will issue the push.

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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