1

I'm trying to figure out the best way to handle this scenario.

I have an MVC back end in Azure that has a bunch of end points for my app. I want to add another end point to handle sending iOS Push Notifications to users.

I am handling storing their device tokens, so my though was to just use PushSharp and loop over each token and push the appropriate token to the user. That works great locally, but I can't seem to get PushSharp to work in Azure. I've seen this solution here:

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

However I am unsure what the ServiceDefinition.csdef file is and how that would be incorporated into my MVC solution.

I've started researching Azure Notification Hub but I'm not sure it can do what I need. I'm just looking for a simple solution in C# that can be hosted in Azure(ASP.NET MVC) where I can loop over a group of device tokens and push notifications.

1 Answer 1

4

If you already have Azure-hosted MVC application which takes and stores device tokens from end user iOS applications, then you could just create Service Bus namespace and Notification Hub in it, upload APNS certificate, and then your MVC application is able to register tokens and send notifications using Microsoft.ServiceBus.dll.

Here is very basic code sample:

 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");

Here I used tag to target the message the particular user. If you call just SendAppleNativeNotificationAsync("{your message}"), then message will be delivered to all users. Learn more about tags and tag expressions to make your sends efficient.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi thanks is there sample code for this? I cannot seem to figure out how to use the device token with the ServiceBus DLL
Make sure you call CreateAppleNativeRegistrationAsync only once for single device. Next time if you want to refresh token or add/remove some tags etc., then you should call UpdateRegistrationAsync.
Thats strange, on an update i wouldn't think you'd have access to to the Registration object since that should only be called once, correct?
nevermind looks like you can just spin up the object to update var regs = new AppleRegistrationDescription("token", new[] { "userid" }); hub.UpdateRegistrationAsync(regs);
You could store registration Id and then just create new exemplar of AppleRegistrationDescription class. Also some people do GetRegistrationsByTagAsync("user id", 100) or GetRegistrationsByChannelAsync("device token", 100) to just retrieve registration from hub, but in fact that call does unnecessary performance hit to your backend and to notification hub itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.