问题
I'm currently replacing PostSharp with Azure Notification Hub as post sharp seems to have stopped working in Azure. I have setup my app and it registers its device tag with Azure. I have tested a push notification from the Azure portal broadcasting to all and it works.
However I want to amend my Web API project so I can send notifications to a specific device. Here the code that I have got so far where device tag is the device tag of the iPhone:
var alert = "{\"aps\":{\"alert\":\"Hello from .NET!\"}}";
await hub.SendAppleNativeNotificationAsync(alert, new List<string> { deviceTag });
This does not work however. I thought I could just send the device tag similar to how post sharp works? I'm a little confused as I know this device has successfully registered with the hub in Azure, because if I do a test broadcast from the portal it receives a notification. How do I amend my API code to send to just a specific device? I came across this article https://azure.microsoft.com/en-gb/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-wns-notification/ but it seems odd, as I already know the device token. Is it possible to send a notification to the hub and specify the device tag?
回答1:
You can definitely send with just the deviceToken. Tags only work if you have registered them with the deviceHandle first. The article you found (and it's siblings discussing Google Cloud/Firebase Messaging and iOS) assume that you're using the Registration / Tags mechanism.
You don't need to do that -- you can use the DirectSend or BatchSend notification mechanisms if you just want to talk directly to deviceTokens. At the "standard" tier you can also use per-message telemetry to find outcomes for calls to Direct or Batch Send.
I started with the Registration oriented examples and then moved to the "direct Send" model -- and we will be developing along those lines. I don't have the iOS implementation working yet, otherwise I'd give you an example with that.
These links may help:
Direct Send
(note that the C# NotificationHubClient
type has a SendDirectNotificationAsync
method that implements the Direct Send model).
Batch Direct Send
Per-Message-Telemetry
Article putting it all together w/ link to GitHub sample
回答2:
To use DirectSend, you need to work with the abstract class defined
Microsoft.Azure.NotificationHubs.Notification
How do you work with this class, and what information needs to be configured for this to work properly?
来源:https://stackoverflow.com/questions/40052104/azure-notification-hubs-send-notification-to-specific-device